64

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

K Scandrett
  • 16,390
  • 4
  • 40
  • 65
cam
  • 8,725
  • 18
  • 57
  • 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

75

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

H H
  • 263,252
  • 30
  • 330
  • 514
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 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
17

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
  • 5,527
  • 7
  • 48
  • 77
  • 1
    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,485
  • 5
  • 48
  • 64
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
  • 242,243
  • 40
  • 408
  • 536
4
ListBox1.DataSource = CreateDataSource();
ListBox1.DataTextField = "FieldProperty";
ListBox1.DataValueField = "ValueProperty";

Please refer to this article for detailed examples.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Maddie
  • 69
  • 1
  • 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
  • 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
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
H H
  • 263,252
  • 30
  • 330
  • 514
-1

For a UWP app:

XAML

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

C#

public ObservableCollection<Type> Results
Samir
  • 144
  • 1
  • 4
  • 12
  • 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