How to: Bind Data to the Windows Forms DataGridView Control Using the Designer

You can use the designer to connect a DataGridView control to data sources of several different varieties, including databases, business objects, or Web services. When you bind the control to a data source using the designer, the control is automatically bound to a BindingSource component that represents the data source. Additionally, columns are automatically generated in the control to match the schema information provided by the data source.

After columns have been generated, you can modify them to meet your needs. For example, you can remove or hide columns you are not interested in displaying, you can rearrange the columns, or you can modify the column types. For more information about modifying columns, see the topics listed in the See Also section.

You can also bind multiple DataGridView controls to related tables to create master/detail relationships. In this configuration, one control displays a parent table and another control displays only those rows from a child table that are related to the current row in the parent table. For more information, see How to: Display Related Data in a Windows Forms Application.

The following procedure requires a Windows Application project with a form that contains a DataGridView control or two controls for a master/detail relationship. For information about starting such a project, see How to: Create a Windows Forms application project and How to: Add Controls to Windows Forms.

To bind the control to a data source

  1. Click the designer actions glyph (Small black arrow) on the upper-right corner of the DataGridView control.

  2. Click the drop-down arrow for the Choose Data Source option.

  3. If your project does not already have a data source, click Add Project Data Source and follow the steps indicated by the wizard.

    For more information, see Data Source Configuration Wizard. Your new data source will appear in the Choose Data Source drop-down window. If your new data source contains only one member, such as a single database table, the control will automatically bind to that member. Otherwise, continue to the next step.

  4. Expand the Other Data Sources and Project Data Sources nodes if they are not already expanded, and then select the data source to bind the control to.

  5. If your data source contains more than one member, such as if you have created a System.Data.DataSet that contains multiple tables, expand the data source, and then select the specific member to bind to.

  6. To create a master/detail relationship, in the Choose Data Source drop-down window for a second DataGridView control, expand the BindingSource created for the parent table, and then select the related child table from the list shown.

    Note

    If your project already has a data source, you can also use the Data Sources window to create a data form. For more information, see Data Sources Window.

See also