What's the simplest way to bind a Listbox to a List of objects in Windows Forms?
8 Answers
You're looking for the DataSource property
:
List<SomeType> someList = ...;
myListBox.DataSource = someList;
You should also set the DisplayMember
property to the name of a property in the object that you want the listbox to display. If you don't, it will call ToString()
.
-
How would I go about removing an item of SomeType from the Listbox via selection? – cam Apr 20 '10 at 12:46
-
-
7Hello. It is working for me as long as I don't add anything to collection. As soon as I change my collection items in list box does not update. Even after assigning dataSource after chaning items in collection. – Hooch Dec 09 '12 at 20:23
-
If you are binding via a viewmodel, then the List must be encapsulated in an ObservableCollection. – SASS_Shooter May 22 '13 at 18:06
-
1
-
13
-
Just had the same question but wondered why there is no `DataSource`... if you use *System.Windows.Controls.ListBox* the equivalent is `ItemSource` – Berger Dec 11 '19 at 01:54
Binding a System.Windows.Forms.Listbox Control to a list of objects (here of type dynamic)
List<dynamic> dynList = new List<dynamic>() {
new {Id = 1, Name = "Elevator", Company="Vertical Pop" },
new {Id = 2, Name = "Stairs", Company="Fitness" }
};
listBox.DataSource = dynList;
listBox.DisplayMember = "Name";
listBox.ValueMember = "Id";

- 5,527
- 7
- 48
- 77
-
1I know this question is not related, but: how would you access the `Company` member? – Wolf May 14 '18 at 10:29
-
@Wolf you can't unless you dig through the list yourself; omitting the ValueMember setting so that the SelectedValue is the entire object would probably be easier – Caius Jard Sep 01 '21 at 07:05
Pretending you are displaying a list of customer objects with "customerName" and "customerId" properties:
listBox.DataSource = customerListObject;
listBox.DataTextField = "customerName";
listBox.DataValueField = "customerId";
listBox.DataBind();
Edit: I know this works in asp.net - if you are doing a winforms app, it should be pretty similar (I hope...)

- 21,485
- 5
- 48
- 64
Granted, this isn't going to provide you anything truly meaningful unless the objects have properly overriden ToString()
(or you're not really working with a generic list of objects and can bind to specific fields):
List<object> objList = new List<object>();
// Fill the list
someListBox.DataSource = objList;

- 242,243
- 40
- 408
- 536
I haven 't seen it here so i post it because for me is the best way in winforms:
List<object> objList = new List<object>();
listBox.DataSource = objList ;
listBox.Refresh();
listBox.Update();

- 555
- 11
- 15
-
Oh, my God...thank you! It's been 12 years since I've worked in WinForms and I've been stuck for almost two hours trying to get this &@%$ ListBox to behave. – Neil T. Apr 11 '21 at 17:22
There are two main routes here:
1: listBox1.DataSource = yourList;
Do any manipulation (Add/Delete) to yourList and Rebind.
Set DisplayMember and ValueMember to control what is shown.
2: listBox1.Items.AddRange(yourList.ToArray());
(or use a for-loop to do Items.Add(...)
)
You can control Display by overloading ToString() of the list objects or by implementing the listBox1.Format event.
For a UWP app:
XAML
<ListBox x:Name="List" DisplayMemberPath="Source" ItemsSource="{x:Bind Results}"/>
C#
public ObservableCollection<Type> Results

- 144
- 1
- 4
- 12
-
-
@LarsTech didn't notice that since it wasn't part of the title or text, but I got here by looking for answers to the same question for UWP, perhaps someone else will too and find this answer useful. – Samir Sep 19 '18 at 18:49