title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Walkthrough: Create a master-detail form using two DataGridView controls |
Learn about how to create a master/detail form using two Windows Forms DataGridView Controls in this walkthrough. |
03/30/2017 |
|
|
c5fa29e8-47f7-4691-829b-0e697a691f36 |
One of the most common scenarios for using the xref:System.Windows.Forms.DataGridView control is the master/detail form, in which a parent/child relationship between two database tables is displayed. Selecting rows in the master table causes the detail table to update with the corresponding child data.
Implementing a master/detail form is easy using the interaction between the xref:System.Windows.Forms.DataGridView control and the xref:System.Windows.Forms.BindingSource component. In this walkthrough, you will build the form using two xref:System.Windows.Forms.DataGridView controls and two xref:System.Windows.Forms.BindingSource components. The form will show two related tables in the Northwind SQL Server sample database: Customers
and Orders
. When you are finished, you will have a form that shows all the customers in the database in the master xref:System.Windows.Forms.DataGridView and all the orders for the selected customer in the detail xref:System.Windows.Forms.DataGridView.
To copy the code in this topic as a single listing, see How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls.
In order to complete this walkthrough, you will need:
- Access to a server that has the Northwind SQL Server sample database.
-
Create a class that derives from xref:System.Windows.Forms.Form and contains two xref:System.Windows.Forms.DataGridView controls and two xref:System.Windows.Forms.BindingSource components. The following code provides basic form initialization and includes a
Main
method. If you use the Visual Studio designer to create your form, you can use the designer generated code instead of this code, but be sure to use the names shown in the variable declarations here.[!code-csharpSystem.Windows.Forms.DataGridViewMasterDetails#01] [!code-vbSystem.Windows.Forms.DataGridViewMasterDetails#01] [!code-csharpSystem.Windows.Forms.DataGridViewMasterDetails#02] [!code-vbSystem.Windows.Forms.DataGridViewMasterDetails#02]
-
Implement a method in your form's class definition for handling the detail of connecting to the database. This example uses a
GetData
method that populates a xref:System.Data.DataSet object, adds a xref:System.Data.DataRelation object to the data set, and binds the xref:System.Windows.Forms.BindingSource components. Be sure to set theconnectionString
variable to a value that is appropriate for your database.[!IMPORTANT] Storing sensitive information, such as a password, within the connection string can affect the security of your application. Using Windows Authentication (also known as integrated security) is a more secure way to control access to a database. For more information, see Protecting Connection Information.
[!code-csharpSystem.Windows.Forms.DataGridViewMasterDetails#20] [!code-vbSystem.Windows.Forms.DataGridViewMasterDetails#20]
-
Implement a handler for your form's xref:System.Windows.Forms.Form.Load event that binds the xref:System.Windows.Forms.DataGridView controls to the xref:System.Windows.Forms.BindingSource components and calls the
GetData
method. The following example includes code that resizes xref:System.Windows.Forms.DataGridView columns to fit the displayed data.[!code-csharpSystem.Windows.Forms.DataGridViewMasterDetails#10] [!code-vbSystem.Windows.Forms.DataGridViewMasterDetails#10]
You can now test the form to make sure it behaves as expected.
-
Compile and run the application.
You will see two xref:System.Windows.Forms.DataGridView controls, one above the other. On top are the customers from the Northwind
Customers
table, and at the bottom are theOrders
corresponding to the selected customer. As you select different rows in the upper xref:System.Windows.Forms.DataGridView, the contents of the lower xref:System.Windows.Forms.DataGridView change accordingly.
This application gives you a basic understanding of the xref:System.Windows.Forms.DataGridView control's capabilities. You can customize the appearance and behavior of the xref:System.Windows.Forms.DataGridView control in several ways:
-
Change border and header styles. For more information, see How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control.
-
Enable or restrict user input to the xref:System.Windows.Forms.DataGridView control. For more information, see How to: Prevent Row Addition and Deletion in the Windows Forms DataGridView Control, and How to: Make Columns Read-Only in the Windows Forms DataGridView Control.
-
Validate user input to the xref:System.Windows.Forms.DataGridView control. For more information, see Walkthrough: Validating Data in the Windows Forms DataGridView Control.
-
Handle very large data sets using virtual mode. For more information, see Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control.
-
Customize the appearance of cells. For more information, see How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control and How to: Set Default Cell Styles for the Windows Forms DataGridView Control.
- xref:System.Windows.Forms.DataGridView
- xref:System.Windows.Forms.BindingSource
- Displaying Data in the Windows Forms DataGridView Control
- How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls
- Protecting Connection Information