Creating Master-Details Lists in DataGrid Control in VB.NET

In this article you will learn how to Create Master-Details Lists with the Windows Forms DataGrid Control.
  • 6654
Creating Master-Details Lists with the Windows Forms DataGrid Contro
 
If your DataSet contains a series of related tables, you can use two DataGrid controls to display the data in a master-detail format. One DataGrid is designated to be the master grid, and the second is designated to be the details grid. When you select 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 would specify the Customers table to be the master grid and the Orders table to be the details grid. When a customer is selected from the master grid, all of the orders associated with that customer in the Orders table would be displayed in the details grid.

To create a master-details list in the designer

  1. Add two DataGrid controls to the form from the Windows Forms tab of the Toolbox.
  2. Drag two or more tables from Server Explorer to the form.
  3. From the Data menu, select Generate DataSet.
  4. Set the relationships between the tables using the XML Designer. For details, see Creating One-to-Many Relationships in XML Schemas and Datasets.
  5. Save the relationships by selecting Save All from the File menu.
  6. Configure the DataGrid control that you want to designate the master grid:
    1. Select the DataSet object from the drop-down list in the DataSource property.
    2. Select the master table (for example, "Customers") from the drop-down list in the DataMember property.
  7. Configure the DataGrid control that you want to designate the details grid:
    1. Select the DataSet object from the drop-down list in the DataSource property.
    2. Select the relationship (for example, "Customers.CustOrd") between the master and detail tables from the drop-down list in the DataMember property. In order to see the relationship, you will have to expand the node by clicking on the + sign next to the master table in the drop-down list.

To set a master-details relationship programmatically

  1. Create two new DataGrid controls and set their properties.
  2. Add tables to the dataset.
  3. Declare a variable of type DataRelation to represent the relation you want to create.
  4. Instantiate the relationship by specifying a name for the relationship and specifying the table, column, and item that will tie the two tables.
  5. Add the relationship to the DataSet object's Relations collection.
  6. Use the SetDataBinding method of the DataGrid to bind each of the grids to the DataSet.

The following example shows how to set a master-details relationship between the Customers and Orders tables in a previously generated DataSet (ds).

        ' Visual Basic
        Dim myDataRelation As DataRelation
        myDataRelation = New DataRelation _
           ("CustOrd", ds.Tables("Customers").Columns("CustomerID"), _
           ds.Tables("Orders").Columns("CustomerID"))
        ' Add the relation to the DataSet.
        ds.Relations.Add(myDataRelation)
        GridOrders.SetDataBinding(ds, "Customers")
        GridDetails.SetDataBinding(ds, "Customers.CustOrd")

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.