This documentation is archived and is not being maintained.

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.
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.

    // 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.

    // Create relationships.


// 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");