| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Create a Master-Detail Form Using Two Windows Forms DataGridView Controls |
03/30/2017 |
.net-framework |
|
article |
|
|
99f6e876-3f7f-4139-9063-e36587c95b02 |
23 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls
The following code example creates a master/detail form using two xref:System.Windows.Forms.DataGridView controls bound to two xref:System.Windows.Forms.BindingSource components. The data source is a xref:System.Data.DataSet that contains the Customers and Orders tables from the Northwind SQL Server sample database along with a xref:System.Data.DataRelation that relates the two through the CustomerID column.
One xref:System.Windows.Forms.BindingSource is bound to the parent Customers table in the data set. This data is displayed in the master xref:System.Windows.Forms.DataGridView control. The other xref:System.Windows.Forms.BindingSource is bound to the first data connector. The xref:System.Windows.Forms.BindingSource.DataMember%2A property of the second xref:System.Windows.Forms.BindingSource is set to the xref:System.Data.DataRelation name. This causes the associated detail xref:System.Windows.Forms.DataGridView control to display the rows of the child Orders table that correspond to the current row in the master xref:System.Windows.Forms.DataGridView control.
For a complete explanation of this code example, see Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls.
Example
[!code-csharpSystem.Windows.Forms.DataGridViewMasterDetails#00] [!code-vbSystem.Windows.Forms.DataGridViewMasterDetails#00]
Compiling the Code
This example requires:
References to the System, System.Data, System.Windows.Forms, and System.XML assemblies.
For information about building this example from the command line for [!INCLUDEvbprvb] or [!INCLUDEcsprcs], see Building from the Command Line or Command-line Building With csc.exe. You can also build this example in [!INCLUDEvsprvs] by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
.NET Framework Security
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.
See Also
xref:System.Windows.Forms.DataGridView
xref:System.Windows.Forms.BindingSource
Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls
Displaying Data in the Windows Forms DataGridView Control
Protecting Connection Information