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
- Searching in Data View
- Extracting array of rows
- 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";