DataViews and Data Binding—Master-Detail Forms


Jump to: navigation, search
Visual C# Tutorials
Database Tutorials

DataViews and Data Binding

© 2003 O'Reilly Media, Inc.

Master-Detail Forms

The PostionChanged event also makes it easy to create master-detail forms (see Figure 12-8). A master-detail binds two data objects and uses two CurrencyManager objects. When the parent record changes, the child data must also be modified. This modification can be accomplished by configuring the DataView.RowFilter property.

Example 12-7. Master-detail data binding

public class MasterDetail : System.Windows.Forms.Form
{
  private System.Windows.Forms.DataGrid gridSuppliers;
  private System.Windows.Forms.DataGrid gridProducts;
  // (Designer code omitted.)
  DataSet ds = new DataSet("Northwind");
  private void MasterDetail_Load(object sender, 
      System.EventArgs e)
  {
  string connectionString = "Data Source=localhost;" +
    "Initial Catalog=Northwind;Integrated Security=SSPI";
  string SQL = "SELECT * FROM Products";
 
  // Create ADO.NET objects.
  SqlConnection con = new SqlConnection(connectionString);
  SqlCommand com = new SqlCommand(SQL, con);
  SqlDataAdapter adapter = new SqlDataAdapter(com);
  // Execute the command.
 
  try
  {
    adapter.Fill(ds, "Products");
    com.CommandText = "SELECT * FROM Suppliers";
    adapter.Fill(ds, "Suppliers");
  }
  finally
  {
    con.Close();
  }
  // Display the results.
  gridSuppliers.DataSource = ds.Tables["Suppliers"].DefaultView;
  gridProducts.DataSource = ds.Tables["Products"].DefaultView;
 
  // Handle the PositionChanged event for the Suppliers table.
  BindingManagerBase currency;
  currency = this.BindingContext[gridSuppliers.DataSource];
  currency.PositionChanged += new
  EventHandler(Binding_PositionChanged);
}
 
private void Binding_PositionChanged(object sender, System.EventArgs e)
{
  string filter;
  DataRowView selectedRow;
 
  // Find the current category row.
  selectedRow = (DataRowView)this.BindingContext[
      gridSuppliers.DataSource].Current;
 
  // Create a filter expression using its SupplierID.
  filter = "SupplierID='" + 
      selectedRow["SupplierID"].ToString() + "'";
 
  // Modify the view onto the product table.
  ds.Tables["Products"].DefaultView.RowFilter = filter;
  }
}

Example 12-7 uses two DataGrid controls: one that displays Suppliers (the parent table) and one that displays the Products offered by the currently selected supplier. The DataGrid controls are bound as before. The difference is that the PositionChanged event handler dynamically builds a filter string based on the currently selected supplier and uses it to filter the product list.


Image:ADONETNutshell12-8.jpg
Figure 12-8. A master-detail form


Another equivalent option is to use the CreateChildView( ) method discussed earlier in this chapter to generate a new DataView object based on a DataRelation each time the position changes.


Previous_Page_.gif Next_Page_.gif

Share this page
  • del.icio.us
  • Facebook
  • Google+
  • StumbleUpon