title | description | ms.date | helpviewer_keywords | ms.assetid | |||
---|---|---|---|---|---|---|---|
Create Master-Details Lists with DataGrid Control Using the Designer |
Learn about how to create master-details lists with the Windows Forms DataGrid control using the designer. |
03/30/2017 |
|
19438ba2-f687-4417-a2fb-ab1cd69d4ded |
Note
The xref:System.Windows.Forms.DataGridView control replaces and adds functionality to the xref:System.Windows.Forms.DataGrid control; however, the xref:System.Windows.Forms.DataGrid control is retained for both backward compatibility and future use, if you choose. For more information, see Differences Between the Windows Forms DataGridView and DataGrid Controls.
If your xref:System.Data.DataSet contains a series of related tables, you can use two xref:System.Windows.Forms.DataGrid controls to display the data in a master-detail format. One xref:System.Windows.Forms.DataGrid is designated to be the master grid, and the second is designated to be the details grid. When you select an entry in the master list, all of the related child entries are shown in the details list. For example, if your xref:System.Data.DataSet contains a Customers table and a related Orders table, you would specify the Customers table to be the master grid and the Orders table to be the details grid. When a customer is selected from the master grid, all of the orders associated with that customer in the Orders table would be displayed in the details grid.
The following procedure requires a Windows Application project (File > New > Project > Visual C# or Visual Basic > Classic Desktop > Windows Forms Application).
-
Add two xref:System.Windows.Forms.DataGrid controls to the form. For more information, see How to: Add Controls to Windows Forms. In Visual Studio 2005, the xref:System.Windows.Forms.DataGrid control is not in the Toolbox by default. For more information, see How to: Add Items to the Toolbox.
[!NOTE] The following steps are not applicable to Visual Studio 2005, which uses the Data Sources window for design-time data binding. For more information, see Bind controls to data in Visual Studio and How to: Display Related Data in a Windows Forms Application.
-
Drag two or more tables from Server Explorer to the form.
-
From the Data menu, select Generate DataSet.
-
Set the relationships between the tables using the XML Designer. For details, see "How to: Create One-to-Many Relationships in XML Schemas and Datasets" on MSDN.
-
Save the relationships by selecting Save All from the File menu.
-
Configure the xref:System.Windows.Forms.DataGrid control that you want to designate the master grid, as follows:
-
Select the xref:System.Data.DataSet from the drop-down list in the xref:System.Windows.Forms.DataGrid.DataSource%2A property.
-
Select the master table (for example, "Customers") from the drop-down list in the xref:System.Windows.Forms.DataGrid.DataMember%2A property.
-
-
Configure the xref:System.Windows.Forms.DataGrid control that you want to designate the details grid, as follows:
-
Select the xref:System.Data.DataSet from the drop-down list in the xref:System.Windows.Forms.DataGrid.DataSource%2A property.
-
Select the relationship (for example, "Customers.CustOrd") between the master and detail tables from the drop-down list in the xref:System.Windows.Forms.DataGrid.DataMember%2A property. In order to see the relationship, expand the node by clicking on the plus (+) sign next to the master table in the drop-down list.
-