This documentation is archived and is not being maintained.

ComboBox Databinding Sample 

Download sample

This sample shows how to bind data to the ComboBox and DataGridView controls.

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To open the sample file in Solution Explorer

  1. Click Download Sample.

    The File Download message box appears.

  2. Click Open, and on the left column of the zip folder window, click Extract all files.

    The Extraction Wizard opens.

  3. Click Next. You can change the directory that the files will be extracted to, and then click Next again.

  4. Make sure that the Show extracted files check box is selected, and click Finish.

  5. Double-click the sample's .sln file.

    The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.

To run this sample

  • Press F5.


This sample requires the Northwind sample database. For more information, see How to: Install and Troubleshoot Database Components for Samples.


The code demonstrates how to bind six different types of data sources to a ComboBox control. Data is bound from:

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 that provides a sorted view of the ProductName column is also created at this time. The Products table is loaded into another DataSet using a TableAdapter and BindingSource.

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 either dataset, or the sorted data view. If the user binds to the dataset, data view 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. If the user binds to the data connector, the combo box and datagridview are in sync. Changing the value of the combo box moves the datagridview to the same record. Scrolling through the dataset in the grid or using the navigation toolbar updates the combo box.

The main form contains the combo box controls, button controls to load data, and two label controls and a grid to display the data source and the selected value. The ArrayList, DataSet, and DataView allow you to associate a value with each item displayed in the combo box control. For example, if the user selects the product entry Chai from the 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 property allows you to select the item that contains the associated value. The DisplayMember property allows you to select the item that is displayed in the combo box control.

Creating this Sample

Most of this form was created by dragging components onto the form, then using smart tags and settings in the Properties window. The following is a quick summary of how you could create the DataGridView portion of the form from scratch:

  1. Create a new Windows Application project.

  2. With Form1 open, select the Data Source window. It can also be activated by means of the Data menu.

  3. In the Data Sources window, click Add New Data Source.

  4. In the Data Source Configuration Wizard, choose Database as the data source type.

  5. For the data connection, choose a server that has Northwind on it.

  6. The following step allows you to save the connection string in a strongly typed application settings file.

    1. In the Choose Your Database Objects, select the Products table.

    2. Click Finish to create your typed dataset for the Northwind database. You can see the results in the Data Sources window.

  7. From the Data Sources window, drag the Products table to Form1.

  8. As a result, you will see a data-bound DataGridView and BindingNavigator controls added to the form designer surface. You will also see the NorthwindDataSet, ProductsTableAdapter, and ProductsBindingSource added to the component tray.

Loading Data Within the Form

In this example, you load the form with data without any parameters provided by the user. Using the DataSet Designer you can to leverage reusable DataAdapters to fill dsProducts2.

When you drag the Employees table from the Data Sources window, Visual Studio automatically places code to call the default query on the TableAdapter in the Form.Load event. In this sample this code was moved to the btnDC Click method:

' Fill the Lookup Tables

See Also