How to bind a List to a ComboBox?

418,936

Solution 1

As you are referring to a combobox, I'm assuming you don't want to use 2-way databinding (if so, look at using a BindingList)

public class Country
{
    public string Name { get; set; }
    public IList<City> Cities { get; set; }
    public Country(string _name)
    {
        Cities = new List<City>();
        Name = _name;
    }
}



List<Country> countries = new List<Country> { new Country("UK"), 
                                     new Country("Australia"), 
                                     new Country("France") };

var bindingSource1 = new BindingSource();
bindingSource1.DataSource = countries;

comboBox1.DataSource = bindingSource1.DataSource;

comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";

To find the country selected in the bound combobox, you would do something like: Country country = (Country)comboBox1.SelectedItem;.

If you want the ComboBox to dynamically update you'll need to make sure that the data structure that you have set as the DataSource implements IBindingList; one such structure is BindingList<T>.


Tip: make sure that you are binding the DisplayMember to a Property on the class and not a public field. If you class uses public string Name { get; set; } it will work but if it uses public string Name; it will not be able to access the value and instead will display the object type for each line in the combo box.

Solution 2

For a backgrounder, there are 2 ways to use a ComboBox/ListBox

1) Add Country Objects to the Items property and retrieve a Country as Selecteditem. To use this you should override the ToString of Country.

2) Use DataBinding, set the DataSource to a IList (List<>) and use DisplayMember, ValueMember and SelectedValue

For 2) you will need a list of countries first

// not tested, schematic:
List<Country> countries = ...;
...; // fill 

comboBox1.DataSource = countries;
comboBox1.DisplayMember="Name";
comboBox1.ValueMember="Cities";

And then in the SelectionChanged,

if (comboBox1.Selecteditem != null)
{
   comboBox2.DataSource=comboBox1.SelectedValue;

}

Solution 3

public MainWindow(){
    List<person> personList = new List<person>();

    personList.Add(new person { name = "rob", age = 32 } );
    personList.Add(new person { name = "annie", age = 24 } );
    personList.Add(new person { name = "paul", age = 19 } );

    comboBox1.DataSource = personList;
    comboBox1.DisplayMember = "name";

    comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
}


void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    person selectedPerson = comboBox1.SelectedItem as person;
    messageBox.Show(selectedPerson.name, "caption goes here");
}

boom.

Share:
418,936

Related videos on Youtube

Mobin
Author by

Mobin

I am a new developer about C# most of the time i am here for solving my project problems and helping as i can ...

Updated on July 08, 2022

Comments

  • Mobin
    Mobin almost 2 years

    I want to connect a BindingSource to a list of class objects and then objects value to a ComboBox.
    Can anyone suggest how to do it?

    public class Country
    {
        public string Name { get; set; }
        public IList<City> Cities { get; set; }
    
        public Country()
        {
            Cities = new List<City>();
        }
    }
    

    is my class and I want to bind its name field to a BindingSource which could be then associated with a ComboBox

    • Mobin
      Mobin about 15 years
      Winforms what i want is help me connect the data values in names field of country object rest i will figure it out
  • Mobin
    Mobin about 15 years
    as well as comboBox1.DataBind(); function i dont see it in solutions I am using winforms
  • Mobin
    Mobin about 15 years
    thanks but a bit of a problem here the Names are not visible in the combobox when running the application
  • demoncodemonkey
    demoncodemonkey over 13 years
    ...which might seem obvious but then everything is obvious in hindsight :)
  • yu_ominae
    yu_ominae about 11 years
    In that case you need to use ToolstripComboBox.ComboBox.DataSource. It looks like ToolstripComboBox is a wrapper for a normal ComboBox.
  • Wade Hatler
    Wade Hatler over 9 years
    This works except the SelectionChanged event doesn't appear to be on the control in .NET 4.0. I replaced that with SelectionChangeCommitted and all is well.
  • beppe9000
    beppe9000 over 8 years
    Can you explain or add bindingSource1's declaration ?
  • 2.718
    2.718 about 8 years
    System.Windows.Forms.BindingSource bindingSource1;
  • Masoud
    Masoud over 5 years
    Is comboBox1.DataSource = bindingSource1.DataSource; correct? or it should be comboBox1.DataSource = bindingSource1;?
  • joe
    joe almost 3 years
    what does one-way binding mean here? does it mean when countries.Add("xxx") (List) from code, the UI will automatically update?