Binding Listbox to List<object> in WinForms
Solution 1
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()
.
Solution 2
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";
Solution 3
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...)
Solution 4
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;
Solution 5
ListBox1.DataSource = CreateDataSource();
ListBox1.DataTextField = "FieldProperty";
ListBox1.DataValueField = "ValueProperty";
Please refer to this article for detailed examples.
cam
Updated on January 09, 2021Comments
-
cam over 3 years
What's the simplest way to bind a Listbox to a List of objects in Windows Forms?
-
cam about 14 yearsHow would I go about removing an item of SomeType from the Listbox via selection?
-
SLaks about 14 years
someList.Remove((SomeType)myListBox.SelectedValue);
(In WinForms) -
Hooch over 11 yearsHello. 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.
-
surfmuggle about 11 yearsThe Windows.Forms.Listbox has other members; see below.
-
SASS_Shooter almost 11 yearsIf you are binding via a viewmodel, then the List must be encapsulated in an ObservableCollection.
-
SLaks almost 11 years@SASS_Shooter: The question is about WinForms.
-
SLaks almost 11 yearsTo handle updates to the collection in WinForms, use
BindingList<T>
. -
Wolf about 6 years@surfmuggle ... and the
DataBind()
method doesn't exist, I guess? -
Wolf about 6 yearsI know this question is not related, but: how would you access the
Company
member? -
LarsTech over 5 yearsQuestion was tagged WinForms.
-
Samir over 5 years@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.
-
Berger over 4 yearsJust had the same question but wondered why there is no
DataSource
... if you use System.Windows.Controls.ListBox the equivalent isItemSource
-
Andrew Truckle over 3 yearsHow do you rebind?
-
Neil T. about 3 yearsOh, 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.
-
Caius Jard over 2 years@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