Microsoft KB Archive/308057

= How To Display Parent and Child Fields Together in a Windows Forms DataGrid by Using Visual Basic .NET =

Article ID: 308057

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q308057





For a Microsoft Visual C# .NET version of this article, see 308454.

IN THIS TASK
SUMMARY
 * Step-by-Step Example

REFERENCES



SUMMARY
This article demonstrates how to display a Windows Forms DataGrid control that is populated with parent and child information from a DataSet object. The sample in this article requires that you add a DataRelation class to DataSet so that DataGrid can navigate between parent and child records. 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

Step-by-Step Example
 Open Visual Studio .NET, and create a new Visual Basic Windows Application. Form1 is created by default. Use the toolbox to add a Button control and a DataGrid control to Form1. 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: Imports System.Data.SqlClient   Add the following code to the Button1_Click event handler: Dim con As New SqlConnection(&quot;server=YourServer;uid=UID;pwd=PWD;database=northwind&quot;) Dim daCust As New SqlDataAdapter(&quot;Select * From Customers Where CustomerID Like 'A%'&quot;, con) Dim daOrders As New SqlDataAdapter(&quot;Select * From Orders Where CustomerID Like 'A%'&quot;, con) Dim ds As New DataSet daCust.Fill(ds, &quot;Cust&quot;) daOrders.Fill(ds, &quot;Orders&quot;) 'Build the relation between Orders and Customers. ds.Relations.Add(&quot;CustOrd&quot;, ds.Tables!Cust.Columns!CustomerID, ds.Tables!Orders.Columns!CustomerID) DataGrid1.DataSource = ds DataGrid1.DataMember = &quot;Cust&quot; </li> 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