|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. ArchiveDisclaimer|
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.
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
Click the smart tag glyph () on the upper-right corner of the DataGridView control.
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 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.
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 a System.Data.DataSet that 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 Data Sources Window.