This documentation is archived and is not being maintained.

ComboBox Databinding Sample

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

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

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.

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.

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