This article describes how to use Web Form controls to display a list of items or records that are associated with a particular item or record that is selected by a user.
If your DataSet object contains a series of related tables, you can use two DataGrid controls to display the data in a master/detail format. One DataSet is designated to be the master grid, and the second is designated to be the details grid. When a user selects an entry in the master list, all of the related child entries are shown in the details list.
For example, if your DataSet contains a customers table and a related orders table, you specify the customers table to be the master grid and the orders table to be the details grid. When a customer is selected in the master grid, all of the orders that are associated with the customer in the orders table are displayed in the details grid.
Right-click Data Connections, and then click Add Connection.
On the Provider tab, click Microsoft OLE DB Provider for SQL Server.
On the Connection tab, type the name of the server that is running Microsoft SQL Server, the user name, and the password to access the server that is running SQL Server, and then select the NorthWind database.
Click Test Connection to verify that the connection works, and then click OK.
Drag the Orders table and the Customers table from Server Explorer to the form. Notice that one SqlConnection object and two SqlDataAdapter objects appear on the form.
On the Data menu, click Generate DataSet. In the dialog box that appears, make sure that all of the tables that you must have for the grid are selected. Make sure that New is selected for the DataSet and that Add this DataSet to the designer is selected. In the New Selection text box, type CustomersOrders to name the DataSet. Notice that a DataSet control appears on the Web Form.
In Project Explorer, double-click the CustomersOrders.xsd file.
Right-click the key field in the Customers master table, point to Add, and then click New Relation.
In the Edit Relation dialog box, under Name, type Relation.
Under Parent element, click Customers. Under Child element, click Orders.
Verify that Key Fields and Foreign Key Fields are set to CustomerID, and then click OK.
Save the relationships by selecting Save All on the File menu.
Right-click WebForm1.aspx, and then click View Code.
Add the following code to the WebForm1.aspx page:
Public Sub FillDataSet(ByVal dataset As MasterDetailTest.CustomersOrders) dataset.EnforceConstraints = False Me.SqlConnection1.Open() Me.SqlDataAdapter1.Fill(dataset) Me.SqlDataAdapter2.Fill(dataset) dataset.EnforceConstraints = True Me.SqlConnection1.Close() End Sub Private Sub Showdetailgrid() If (Me.DataGrid1.SelectedIndex <> -1) Then Dim parentrows As System.Data.DataView Dim childrows As System.Data.DataView Dim currentparentrow As System.Data.DataRowView Me.CustomerOrders1 = CType(Application("CustomersOrders1"),MasterDetailTest.CustomersOrders) parentrows = New DataView() parentrows.Table = Me.CustomersOrders1.Tables("Customers") currentparentrow = parentrows(Me.DataGrid1.SelectedIndex) childrows = currentparentrow.CreateChildView("relation") Me.DataGrid2.DataSource = childrows Me.DataGrid2.DataBind() Me.DataGrid2.Visible = True Else Me.DataGrid2.Visible = False End If End Sub Private Sub DataGrid1_SelectedIndexChanged(ByVal sender AsSystem.Object, ByVal e As System.EventArgs) HandlesDataGrid1.SelectedIndexChanged Me.Showdetailgrid() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click Me.LoadDataSet() Me.DataGrid1.SelectedIndex = -1 Me.DataGrid1.DataBind() Me.DataGrid2.Visible = False Application("CustomersOrders1") = Me.CustomersOrders1 End Sub'Create a new DataSet to hold the records returned from the call to FillDataSet().'A temporary dataset is used, because filling the existing DataSet would'require the databindings to be rebound. Public Sub LoadDataSet() Dim objdatasettemp As MasterDetailTest.CustomersOrders objdatasettemp = New MasterDetailTest.CustomersOrders() Me.FillDataSet(objdatasettemp) CustomersOrders1.Clear() CustomersOrders1.Merge(objdatasettemp) End Sub