|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Bind Data to the Windows Forms DataGridView Control Using the Designer
You can use the designer to connect acontrol 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 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.
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, seeand .
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.
To bind the control to a data source
On the control's smart tag, click the drop-down arrow for the Choose Data Source option.
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. 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.
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.
If your data source contains more than one member, such as if you have created athat contains multiple tables, expand the data source, and then select the specific member to bind to.
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.
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.