管理 DataViews (ADO .NET)
您可以使用 DataViewManager 管理 DataView 內所有資料表的檢視設定。 如果您想將某個控制項繫結至多個資料表,例如巡覽關聯性的方格,DataViewManager 是理想的選擇。
DataViewManager 包含 DataViewSetting 物件的集合,這些物件可用來設定 DataSet 之資料表的檢視設定。 在 DataSet 中,每一個資料表的 DataViewSettingCollection 都會包含一個 DataViewSetting 物件。 您可以使用參考資料表的 DataViewSetting,來設定參考資料表的預設 ApplyDefaultSort、Sort、RowFilter 和 RowStateFilter 屬性。 您可以參考特定資料表的 DataViewSetting,方法是透過名稱或序數參考,或將參考傳遞給這個特定資料表物件; 此外,您還可以使用 DataViewSettings 屬性來存取 DataViewManager 中的 DataViewSetting 物件集合。
下列程式碼範例以 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");