Bagikan melalui


How to: Create Master/Detail Lists with the Windows Forms DataGrid Control

Note

The DataGridView control replaces and adds functionality to the DataGrid control; however, the DataGrid control is retained for both backward compatibility and future use, if you choose. For more information, see Differences Between the Windows Forms DataGridView and DataGrid Controls.

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 set a master/detail 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/detail relationship between the Customers and Orders tables in a previously generated DataSet (ds).

    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")
    
    DataRelation myDataRelation;
    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");
    
    DataRelation^ myDataRelation;
    myDataRelation = gcnew 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");
    

See Also

Tasks

How to: Bind the Windows Forms DataGrid Control to a Data Source

Reference

DataGrid Control Overview (Windows Forms)

Other Resources

DataGrid Control (Windows Forms)