Data Access: Bind Data to a ComboBox

This sample shows some how to bind data to a combo box control . 

Featured Highlights:

The code demonstrates how to bind data to a combo box from a variety of different data sources. Data is bound from an array, array list, datatable and dataview. When the main form loads, the products table from the Northwind database is retrieved into a dataset using a simple SQL Select statement. A dataview which provides a sorted view of the ProductName column is also created at this time. The user can then populate the combo box control by binding to another array of colors, an array list of shapes, an advanced array list containing sales divisions defined with a structure, the products table residing in the dataset, or the sorted dataview. If the user binds to the dataset, dataview or the advanced array list of sales divisions, when an entry is selected from the combo box, an associated value for that entry is also displayed.

Requirements:

Running the Sample

Simply press F5.

  1. The advanced array list, dataset and dataview binding displays one set of values, but also associates a corresponding values that are available when an entry is selected. For example if the user selects the product entry Chai from products table bound to the combo box, Chai is displayed as the selected entry, but its associated ProductId is also available through the selectedValue property. The ValueMember propery of the combo box allows an alternate value to be made available.
  2. When the datasource property is used to bind data to a combo box, the sorted property is unavailable to ensure that the data is displayed in sorted order. The data source itself must be sorted.

See Also:

Data Binding a Windows Forms ComboBox, ListBox, or CheckedListBox Control Providers of Data to Windows Forms ComboBox Binding Sample , ComboBox Class