Set IList as ComboBox Datasource in C#

ComboBox is a control which let user input or select one of value from listed items .In C#.Net we can easily populate data to combo from database without complicated loops. All you need to pass the display, value member and a data table as follows

cmb_handle.DataSource = Common.MaccDataset.Tables[0];

cmb_handle.DisplayMember = “product”;

cmb_handle.ValueMember = “pid”;
we can also use IList, which can store values of variant types as data source. In this example I will show you

  1. Searching in Data View
  2. Extracting array of rows
  3. Storing arrays of rows to IList

Searching the Data View

First we need to setup the adapter ,fill dataset and data view, I skipped those steps to avoid repetition, you can refer my past posts if you are not familiar with Data objects. Data View contain data from table, we can make use of Find() or findRow() to search the Data view.

Common.AccountRegistrationTableView.Sort = “grpid”;

DataRowView [] s = Common.AccountRegistrationTableView.FindRows(5);

The DataRowView array stores all rows which matched the group id 5 (5 for suppliers in our database).

Copy rows to IList and add it to the data source

The IList interface implements generic Lists which can store any type of data. We need to store DataRowView objects using Tolist () method of the DataRowView class.

IList<DataRowView> rs=s.ToList<DataRowView>();

Now we can set the Datasource and the Display memeber

cmb_supplier.DataSource = rs;

cmb_supplier.DisplayMember = “name”;

The complete code

Common.AccountRegistrationTableView.Sort = "grpid";

 DataRowView [] s = Common.AccountRegistrationTableView.FindRows(Common.getGroupID("Suppliers"));

IList<DataRowView> rs=s.ToList<DataRowView>();

cmb_supplier.DataSource = rs;

 cmb_supplier.DisplayMember = "name";


Published by


A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.