管理 DataView (ADO.NET)

可以使用 DataViewManager 来管理 DataView 中所有表的视图设置。 如果有一个控件(例如进行关系导航的网格)要绑定到多个表,DataViewManager 则是理想的选择。

DataViewManager 包含 DataViewSetting 对象的集合,这些对象用于设置 DataSet 中各表的视图设置。 对于 DataSet 中的每个表,DataViewSettingCollection 都包含一个 DataViewSetting 对象。 可以使用所引用表的 DataViewSetting 来设置该表的默认 ApplyDefaultSortSortRowFilterRowStateFilter 属性。 可以按名称或序号引用或通过向特定表对象传递引用来引用该特定表的 DataViewSetting。 可以使用 DataViewSettings 属性来访问 DataViewManagerDataViewSetting 对象的集合。

以下代码示例使用 SQL Server Northwind 数据库的 Customers 表、Orders 表和 Order Details 表来填充 DataSet,创建表之间的关系,使用 DataViewManager 设置默认 DataView 设置,并将 DataGrid 绑定到 DataViewManager。 该示例将 DataSet 中所有表的默认 DataView 设置设为按表的主键进行排序 (ApplyDefaultSort = true),然后将 Customers 表的排序顺序修改为按 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");

请参见

参考

DataSet

DataViewManager

DataViewSetting

DataViewSettingCollection

其他资源

DataView (ADO.NET)