-->

Thursday, October 31, 2013

How to Bind ComboBox with List of Items: C#

How to Bind ComboBox with List of Items: C#

A list of items is used to store some temporary records, used in the program and do some operations like sorting, grouping and etc. In the previous post we have created a list and bind that list to the datagridview, using its DataSource property.

A combo box also have DataSource property to bind some data as I have discussed in my earlier article i.e. How to bind combobox with database. Combo Box is used to display a single field at a time and a list can contains multiple fields. I will use the same student class as in my earlier articles.
To show a single field, combo box have a property DisplayMember that will specify the field to display. In the following C# code the student list will bind with the combo box and name field will be shown.

List<Student> stuList = new List<Student>();
cmbBox.DataSource = stuList;
cmbBox.DisplayMember = "Name";

Before running this code, you have to sure that your list have some records to show. It will not throw an exception, but it will also not show any records because the list is empty.

Run the project and combo box will bind the list items and we can select any of them. The image shown the combo box items:

How to bind combobox with list of items: C# Windows forms

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved