Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a List to a ComboBox?

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

like image 677
Mobin Avatar asked Mar 01 '09 23:03

Mobin


People also ask

How do you populate a ComboBox in a list C#?

get the listings from your load method 2. loop through them 3. Access my combo box to populate the box with the times from the listing.

Which method is used to add items in ComboBox even?

An entire array can be added to the ComboBox by using the AddRange method to add the object or string of items to the C1ComboBox. To add items to the C1ComboBox using the Add method of the C1ComboBox class. The collection is referenced using the Items property.

What is BindingList in C#?

BindingList is a generic list type that has additional binding support. While you can bind to a generic list, BindingList provides additional control over list items, i.e. if they can be edited, removed or added. BindingList also surfaces events that notify when the list has been changed.


2 Answers

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.

like image 109
Mitch Wheat Avatar answered Sep 28 '22 05:09

Mitch Wheat


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;  } 
like image 24
Henk Holterman Avatar answered Sep 28 '22 04:09

Henk Holterman