可以使用 DataViewManager 来管理 DataSet 中所有表的视图设置。如果有一个控件(例如进行关系导航的网格)要绑定到多个表,DataViewManager 则是理想的选择。
DataViewManager 包含 DataViewSetting 对象的集合,这些对象用于设置 DataSet 中各表的视图设置。对于 DataSet 中的每个表,DataViewSettingCollection 都包含一个 DataViewSetting 对象。可以使用所引用表的 DataViewSetting 来设置该表的默认 ApplyDefaultSort、Sort、RowFilter 和 RowStateFilter 属性。可以按名称或序号引用或通过向特定表对象传递引用来引用该特定表的 DataViewSetting。可以使用 DataViewSettings 属性来访问 DataViewManager 中 DataViewSetting 对象的集合。
以下代码示例使用 Microsoft SQL Server 7.0 Northwind 数据库的 Customers 表、Orders 表和 Order Details 表来填充 DataSet,创建表之间的关系,使用 DataViewManager 设置默认 DataView 设置,并将 DataGrid 绑定到 DataViewManager。该示例将 DataSet 中所有表的默认 DataView 设置设为按表的主键进行排序 (ApplyDefaultSort = true),然后将 Customers 表的排序顺序修改为按 CompanyName 排序。
[Visual Basic]
' Create a Connection, DataAdapters, and a DataSet.
Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated
Security=SSPI;Initial Catalog=northwind")
Dim custDA As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID,
CompanyName FROM Customers", nwindConn)
Dim orderDA As SqlDataAdapter = New SqlDataAdapter("SELECT OrderID,
CustomerID FROM Orders", nwindConn)
Dim ordDetDA As SqlDataAdapter = New SqlDataAdapter("SELECT OrderID,
ProductID, Quantity FROM [Order Details]", nwindConn)
Dim custDS As DataSet = New DataSet()
' Open the Connection.
nwindConn.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.
nwindConn.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 myDVM As DataViewManager = New DataViewManager(custDS)
Dim myDVS As DataViewSetting
For Each myDVS In myDVM.DataViewSettings
myDVS.ApplyDefaultSort = True
Next
myDVM.DataViewSettings("Customers").Sort = "CompanyName"
' Bind to a DataGrid.
Dim myGrid As System.Windows.Forms.DataGrid = New System.Windows.Forms.DataGrid()
myGrid.SetDataBinding(myDVM, "Customers")
[C#]
// Create a Connection, DataAdapters, and a DataSet.
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated
Security=SSPI;Initial Catalog=northwind");
SqlDataAdapter custDA = new SqlDataAdapter("SELECT CustomerID, CompanyName
FROM Customers", nwindConn);
SqlDataAdapter orderDA = new SqlDataAdapter("SELECT OrderID, CustomerID
FROM Orders", nwindConn);
SqlDataAdapter ordDetDA = new SqlDataAdapter("SELECT OrderID, ProductID,
Quantity FROM [Order Details]", nwindConn);
DataSet custDS = new DataSet();
// Open the Connection.
nwindConn.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.
nwindConn.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 myDVM = new DataViewManager(custDS);
foreach (DataViewSetting myDVS in myDVM.DataViewSettings)
myDVS.ApplyDefaultSort = true;
myDVM.DataViewSettings["Customers"].Sort = "CompanyName";
// Bind to a DataGrid.
System.Windows.Forms.DataGrid myGrid = new System.Windows.Forms.DataGrid();
myGrid.SetDataBinding(myDVM, "Customers");