Skip to content

Latest commit

 

History

History
83 lines (54 loc) · 7.84 KB

creating-a-master-detail-form-using-two-datagridviews.md

File metadata and controls

83 lines (54 loc) · 7.84 KB
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
csharp
vb
DataGridView control [Windows Forms], master/detail form
parent-child tables [Windows Forms], displaying on Windows Forms
master-details lists [Windows Forms], displaying on Windows Forms
walkthroughs [Windows Forms], DataGridView control
c5fa29e8-47f7-4691-829b-0e697a691f36

Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls

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.

Prerequisites

In order to complete this walkthrough, you will need:

  • Access to a server that has the Northwind SQL Server sample database.

Creating the form

To create a master/detail form

  1. 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]

  2. 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 the connectionString 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]

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

Testing the Application

You can now test the form to make sure it behaves as expected.

To test the form

  • 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 the Orders 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.

Next Steps

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:

See also