Export (0) Print
Expand All

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 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 Application Project and How to: Add Controls to Windows Forms.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To bind the control to a data source

  1. On the control's smart tag, click the drop-down arrow for the Choose Data Source option.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

    NoteNote

    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

Community Additions

ADD
Show:
© 2014 Microsoft