How to bind a List to a ComboBox?
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.
Related videos on Youtube
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, 2022Comments
-
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 about 15 yearsWinforms what i want is help me connect the data values in names field of country object rest i will figure it out
-
-
Mobin about 15 yearsas well as comboBox1.DataBind(); function i dont see it in solutions I am using winforms
-
Mobin about 15 yearsthanks but a bit of a problem here the Names are not visible in the combobox when running the application
-
demoncodemonkey over 13 years...which might seem obvious but then everything is obvious in hindsight :)
-
yu_ominae about 11 yearsIn that case you need to use
ToolstripComboBox.ComboBox.DataSource
. It looks likeToolstripComboBox
is a wrapper for a normalComboBox
. -
Wade Hatler over 9 yearsThis 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 over 8 yearsCan you explain or add
bindingSource1
's declaration ? -
2.718 about 8 yearsSystem.Windows.Forms.BindingSource bindingSource1;
-
Masoud over 5 yearsIs
comboBox1.DataSource = bindingSource1.DataSource;
correct? or it should becomboBox1.DataSource = bindingSource1;
? -
joe almost 3 yearswhat does one-way binding mean here? does it mean when
countries.Add("xxx")
(List) from code, the UI will automatically update?