Udostępnij za pośrednictwem


Managing DataViews (ADO.NET)

You can use a DataViewManager to manage view settings for all the tables in a DataView. If you have a control that you want to bind to multiple tables, such as a grid that navigates relationships, a DataViewManager is ideal.

The DataViewManager contains a collection of DataViewSetting objects that are used to set the view setting of the tables in the DataSet. The DataViewSettingCollection contains one DataViewSetting object for each table in a DataSet. You can set the default ApplyDefaultSort, Sort, RowFilter, and RowStateFilter properties of the referenced table by using its DataViewSetting. You can reference the DataViewSetting for a particular table by name or ordinal reference, or by passing a reference to that specific table object. You can access the collection of DataViewSetting objects in a DataViewManager by using the DataViewSettings property.

The following code example fills a DataSet with the SQL Server Northwind database tables Customers, Orders, and Order Details, creates the relationships between the tables, uses a DataViewManager to set default DataView settings, and binds a DataGrid to the DataViewManager. The example sets the default DataView settings for all tables in the DataSet to sort by the primary key of the table (ApplyDefaultSort = true), and then modifies the sort order of the Customers table to sort by CompanyName.

' Assumes connection is a valid SqlConnection to Northwind.
' Create a Connection, DataAdapters, and a DataSet.
Dim custDA As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT CustomerID, CompanyName FROM Customers", connection)
Dim orderDA As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT OrderID, CustomerID FROM Orders", connection)
Dim ordDetDA As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT OrderID, ProductID, Quantity FROM [Order Details]", connection)

Dim custDS As DataSet = New DataSet()

' Open the Connection.
connection.Open()

    ' Fill the DataSet with schema information and data.
    custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
    orderDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
    ordDetDA.MissingSchemaAction = MissingSchemaAction.AddWithKey

    custDA.Fill(custDS, "Customers")
    orderDA.Fill(custDS, "Orders")
    ordDetDA.Fill(custDS, "OrderDetails")

    ' Close the Connection.
    connection.Close()

    ' Create relationships.
    custDS.Relations.Add("CustomerOrders", _
          custDS.Tables("Customers").Columns("CustomerID"), _
          custDS.Tables("Orders").Columns("CustomerID"))

    custDS.Relations.Add("OrderDetails", _
          custDS.Tables("Orders").Columns("OrderID"), _
          custDS.Tables("OrderDetails").Columns("OrderID"))

' Create default DataView settings.
Dim viewManager As DataViewManager = New DataViewManager(custDS)

Dim viewSetting As DataViewSetting
For Each viewSetting In viewManager.DataViewSettings
  viewSetting.ApplyDefaultSort = True
Next

viewManager.DataViewSettings("Customers").Sort = "CompanyName"

' Bind to a DataGrid.
Dim grid As System.Windows.Forms.DataGrid = New System.Windows.Forms.DataGrid()
grid.SetDataBinding(viewManager, "Customers")
// Assumes connection is a valid SqlConnection to Northwind.
// Create a Connection, DataAdapters, and a DataSet.
SqlDataAdapter custDA = new SqlDataAdapter(
  "SELECT CustomerID, CompanyName FROM Customers", connection);
SqlDataAdapter orderDA = new SqlDataAdapter(
  "SELECT OrderID, CustomerID FROM Orders", connection);
SqlDataAdapter ordDetDA = new SqlDataAdapter(
  "SELECT OrderID, ProductID, Quantity FROM [Order Details]", connection);

DataSet custDS = new DataSet();

// Open the Connection.
connection.Open();

    // Fill the DataSet with schema information and data.
    custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    orderDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    ordDetDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    custDA.Fill(custDS, "Customers");
    orderDA.Fill(custDS, "Orders");
    ordDetDA.Fill(custDS, "OrderDetails");

    // Close the Connection.
    connection.Close();

    // Create relationships.
    custDS.Relations.Add("CustomerOrders",
          custDS.Tables["Customers"].Columns["CustomerID"],
          custDS.Tables["Orders"].Columns["CustomerID"]);

    custDS.Relations.Add("OrderDetails",
          custDS.Tables["Orders"].Columns["OrderID"],
          custDS.Tables["OrderDetails"].Columns["OrderID"]);

// Create default DataView settings.
DataViewManager viewManager = new DataViewManager(custDS);

foreach (DataViewSetting viewSetting in viewManager.DataViewSettings)
  viewSetting.ApplyDefaultSort = true;

viewManager.DataViewSettings["Customers"].Sort = "CompanyName";

// Bind to a DataGrid.
System.Windows.Forms.DataGrid grid = new System.Windows.Forms.DataGrid();
grid.SetDataBinding(viewManager, "Customers");

See Also

Reference

DataSet

DataViewManager

DataViewSetting

DataViewSettingCollection

Other Resources

DataViews (ADO.NET)

ADO.NET Managed Providers and DataSet Developer Center