Binding Listbox to List<object> in WinForms

179,887

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.

Share:
179,887
cam
Author by

cam

Updated on January 09, 2021

Comments

  • cam
    cam over 3 years

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

  • cam
    cam about 14 years
    How would I go about removing an item of SomeType from the Listbox via selection?
  • SLaks
    SLaks about 14 years
    someList.Remove((SomeType)myListBox.SelectedValue); (In WinForms)
  • Hooch
    Hooch over 11 years
    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.
  • surfmuggle
    surfmuggle about 11 years
    The Windows.Forms.Listbox has other members; see below.
  • SASS_Shooter
    SASS_Shooter almost 11 years
    If you are binding via a viewmodel, then the List must be encapsulated in an ObservableCollection.
  • SLaks
    SLaks almost 11 years
    @SASS_Shooter: The question is about WinForms.
  • SLaks
    SLaks almost 11 years
    To handle updates to the collection in WinForms, use BindingList<T>.
  • Wolf
    Wolf about 6 years
    @surfmuggle ... and the DataBind() method doesn't exist, I guess?
  • Wolf
    Wolf about 6 years
    I know this question is not related, but: how would you access the Company member?
  • LarsTech
    LarsTech over 5 years
    Question was tagged WinForms.
  • Samir
    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
    Berger over 4 years
    Just had the same question but wondered why there is no DataSource... if you use System.Windows.Controls.ListBox the equivalent is ItemSource
  • Andrew Truckle
    Andrew Truckle over 3 years
    How do you rebind?
  • Neil T.
    Neil T. about 3 years
    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.
  • Caius Jard
    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