Thursday, April 26, 2007

Tutorial about Binding with ComboBox



Let say that you have a list of Person that you would like to display into a combobox. To fill a combo box with C#2.0 is pretty simple. First of all, you required a List of strong typed object, for our example a list of Person. Second, you need to bind the list to the component. Finally, you require to tell what's to display and what to hold for value.




Code:
List lstPerson = new List();
lstPerson.Add(new Person("Mary"));
lstPerson.Add(new Person("Sam"));
lstPerson.Add(new Person("Joe"));
this.cboPersons.DataSource = lstPerson;
this.cboPersons.DisplayMember = "Name";
this.cboPersons.ValueMember = "Name";




The final result is simply a combobox with all the three names.


No comments: