63

What's the simplest way to bind a Listbox to a List of objects in Windows Forms?

K Scandrett
  • 15,730
  • 4
  • 36
  • 63
cam
  • 8,465
  • 17
  • 55
  • 81
  • 8
    what is your platform? silverlight? WPF? Winforms? ASP.NET? the answer kinda depends on this knowledge. – Muad'Dib Apr 20 '10 at 12:39

8 Answers8

72

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().

Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
  • How would I go about removing an item of SomeType from the Listbox via selection? – cam Apr 20 '10 at 12:46
  • `someList.Remove((SomeType)myListBox.SelectedValue);` (In WinForms) – SLaks Apr 20 '10 at 12:50
  • 7
    Hello. 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
    @SASS_Shooter: The question is about WinForms. – SLaks May 22 '13 at 18:29
  • 13
    To handle updates to the collection in WinForms, use `BindingList`. – SLaks May 22 '13 at 18:30
  • 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
18

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";  
surfmuggle
  • 4,899
  • 6
  • 42
  • 71
  • I 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
17

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...)

Ray
  • 21,095
  • 5
  • 46
  • 61
6

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;
Justin Niessner
  • 236,029
  • 38
  • 403
  • 530
4
ListBox1.DataSource = CreateDataSource();
ListBox1.DataTextField = "FieldProperty";
ListBox1.DataValueField = "ValueProperty";

Please refer to this article for detailed examples.

ΩmegaMan
  • 26,526
  • 10
  • 91
  • 107
Maddie
  • 69
  • 7
3

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();            
paulofer85
  • 495
  • 9
  • 14
  • 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
2

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.

Community
  • 1
  • 1
Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
-1

For a UWP app:

XAML

<ListBox x:Name="List" DisplayMemberPath="Source" ItemsSource="{x:Bind Results}"/>

C#

public ObservableCollection<Type> Results
Samir
  • 144
  • 1
  • 5
  • 11
  • Question was tagged WinForms. – LarsTech Sep 19 '18 at 18:40
  • @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