Microsoft KB Archive/308484

= How To Display Parent and Child Records in a Windows Forms DataGrid by Using Visual C# .NET =

Article ID: 308484

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q308484



For a Microsoft Visual Basic .NET version of this article, see 308052.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data.SqlClient

IN THIS TASK

 * SUMMARY
 * Requirements
 * Understanding the CurrencyManager
 * Create the Project
 * Test the Application
 * REFERENCES



SUMMARY
This article describes how to create a Windows form that displays a parent (or master) record and all of the related child (or detail) records by using the Northwind Customers and Orders tables. This article also describes the CurrencyManager object and its purpose.

In this article, the parent record information appears in TextBox controls, and the child record information appears in a DataGrid control. The project that you create in this article also contains Button controls so that you can browse through the records.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
 * Microsoft SQL Server version 7.0 or 2000, or Microsoft Data Engine (MSDE) with the Northwind sample database installed
 * Microsoft Visual Studio .NET

back to the top

Understanding the CurrencyManager
It is important that you understand the function of the CurrencyManager object to help you follow the sample in this article. Any data source that you bind to in a Windows Forms control contains an associated CurrencyManager object. The CurrencyManager object monitors the position and otherwise supervises bindings to that data source. The form contains a CurrencyManager object for each discrete data source to which you are binding. If all of the controls on the form bind to a single source (for example, if several TextBox controls are bound to the same data table), they share the same CurrencyManager. When the controls on the form are bound to different sources, the form contains multiple CurrencyManager objects, each of which tracks which record or data element the controls are using.

back to the top

Create the Project
This section describes how to create the project that demonstrates this functionality.  Start Visual Studio .NET, and create a new Visual C# Windows Application project. Add three TextBox controls, five Button controls, and one DataGrid control to the default form. Change the Text properties of the Button controls to Fill, First , Next , Previous , and Last .  Switch to Code view, and add the following statement to the top of the Code window: using System.Data.SqlClient;   Add the following member variable to the Form1 class after the private statements: CurrencyManager cm;   Switch to Form view, and double-click Fill. Add the following code to the Click event: { // Get the data. SqlConnection con = new SqlConnection(&quot;server=;uid=<your_user_id>;                                      pwd=<your_password>;database=northwind&quot;); SqlDataAdapter daCust = new SqlDataAdapter(&quot;Select * from Customers&quot;, con); SqlDataAdapter daOrders = new SqlDataAdapter(&quot;Select * from Orders&quot;,con); DataSet ds=new DataSet; daCust.Fill(ds,&quot;Cust&quot;); daOrders.Fill(ds,&quot;Orders&quot;); ds.Relations.Add(&quot;CustOrd&quot;,ds.Tables[&quot;Cust&quot;].Columns[&quot;CustomerID&quot;], ds.Tables[&quot;Orders&quot;].Columns[&quot;CustomerID&quot;]); // //Bind the controls. // textBox1.DataBindings.Add(&quot;Text&quot;, ds.Tables[&quot;Cust&quot;],&quot;CustomerID&quot;); textBox2.DataBindings.Add(&quot;Text&quot;, ds.Tables[&quot;Cust&quot;],&quot;CompanyName&quot;); textBox3.DataBindings.Add(&quot;Text&quot;, ds.Tables[&quot;Cust&quot;],&quot;ContactName&quot;); dataGrid1.DataSource=ds.Tables[&quot;Cust&quot;]; dataGrid1.DataMember=&quot;CustOrd&quot;; // //Initialize the CurrencyManager. // cm=(CurrencyManager)this.BindingContext[ds.Tables[&quot;Cust&quot;]]; }                   </li> Modify the connection string as appropriate for your environment.</li>  Double-click First, and add the following code to the Click event: { if((cm!=null)&(cm.Count>0)) cm.Position=0; }                   </li>  Double-click Next, and add the following code to the Click event: { if((cm!=null) &(cm.Count>0)&(cm.Position<cm.Count-1)) cm.Position +=1;

} </li>  Double-click Previous, and add the following code to the Click event: { if((cm!=null)&(cm.Count>0)&(cm.Position>0)) cm.Position -=1; }                   </li>  Double-click Last, and add the following code to the Click { if((cm!=null)& (cm.Count>0)) cm.Position=cm.Count;

} </li></ol>

back to the top

Test the Application

 * 1) Press the F5 key to compile and run the application. Note that the form is initially empty.
 * 2) Click Fill to load and bind the data, and use the navigation buttons to move through the data.

back to the top

<div class="references_section">