Microsoft KB Archive/308454

= How To Display Parent and Child Fields Together in a DataGrid by Using Visual C# .NET =

Article ID: 308454

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q308454



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

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

IN THIS TASK
SUMMARY
 * Steps to Build the Sample



SUMMARY
This step-by-step article shows you how to display a DataGrid control that is populated with parent and child information from a relational dataset. As you move through the parent DataSet, you can click the plus sign (+) to the left of the DataGrid to view the related child records.

back to the top

Steps to Build the Sample
 Open Microsoft Visual Studio .NET, and create a new Visual C# Windows Application project. Use the toolbox to add a Button control and a DataGrid control to the default form. In the Properties window, change the Text property of the button to Load . Double-click Load to add an event handler.  Add the following code to the top of the Code window: using System.Data.SqlClient;   Add the following code to the Button1_Click event handler: SqlConnection con = new SqlConnection(&quot;server=haroldo2;uid=sa;&quot; +                                       &quot;pwd=Password1;database=northwind&quot;); SqlDataAdapter daCust = new SqlDataAdapter(&quot;Select * From Customers Where CustomerID Like 'A%'&quot;, con); SqlDataAdapter daOrders = new SqlDataAdapter(&quot;Select * From Orders Where CustomerID Like 'A%'&quot;, con); DataSet ds = new DataSet; daCust.Fill(ds, &quot;Cust&quot;); daOrders.Fill(ds, &quot;Orders&quot;); //Creates the relationship. ds.Relations.Add(&quot;CustOrd&quot;, ds.Tables[&quot;Cust&quot;].Columns[&quot;CustomerID&quot;],                            ds.Tables[&quot;Orders&quot;].Columns[&quot;CustomerID&quot;]); dataGrid1.DataSource = ds; dataGrid1.DataMember = &quot;Cust&quot;;  Modify the SqlConnection string to point to a valid Microsoft SQL Server database.</li> Press the F5 key to compile and run the application.</li> Notice that the grid is empty initially. Click Load to populate the grid.</li> Click the plus sign (+) to display the links to the child records. In this example, one child relation appears. Click the relation to display the child records.</li> In the upper right corner of the grid, click the back arrow to return to the parent records.</li></ol>

back to the top

Keywords: kbhowtomaster kbctrl KB308454

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.