How To Display Parent and Child Fields Together in a Windows Forms DataGrid by Using Visual Basic .NET (308057)
The information in this article applies to:
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
This article was previously published under Q308057 For a Microsoft Visual C# .NET version of this article, see 308454.
IN THIS TASKSUMMARY
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("server=YourServer;uid=UID;pwd=PWD;database=northwind")
Dim daCust As New SqlDataAdapter("Select * From Customers Where CustomerID Like 'A%'", con)
Dim daOrders As New SqlDataAdapter("Select * From Orders Where CustomerID Like 'A%'", con)
Dim ds As New DataSet()
daCust.Fill(ds, "Cust")
daOrders.Fill(ds, "Orders")
'Build the relation between Orders and Customers.
ds.Relations.Add("CustOrd", ds.Tables!Cust.Columns!CustomerID, ds.Tables!Orders.Columns!CustomerID)
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Cust"
- Modify the SqlConnection string to point to a valid Microsoft SQL Server database.
- Press the F5 key to compile and run the application.
- Notice that the grid is empty initially. Click Load to populate the grid.
- 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.
- In the upper right corner of the grid, click the back arrow to return to the parent records.
back to the top
REFERENCES
For more information, refer to the "Walkthrough: Creating a Master-Detail Windows Form" topic in the Visual Studio .NET online help.
back to the top
Modification Type: | Minor | Last Reviewed: | 7/15/2004 |
---|
Keywords: | kbHOWTOmaster KB308057 kbAudDeveloper |
---|
|