Správa zobrazení datManaging DataViews

Můžete použít DataViewManager ke správě nastavení zobrazení pro všechny tabulky DataViewv.You can use a DataViewManager to manage view settings for all the tables in a DataView. Pokud máte ovládací prvek, který chcete vytvořit z více tabulek, jako je například mřížka, která naviguje mezi relacemi, je objekt DataViewManager ideální.If you have a control that you want to bind to multiple tables, such as a grid that navigates relationships, a DataViewManager is ideal.

DataViewSetting Objekt DataViewManager obsahuje kolekci objektů, které se používají k nastavení nastavení zobrazení tabulek v DataSet.The DataViewManager contains a collection of DataViewSetting objects that are used to set the view setting of the tables in the DataSet. Obsahuje jeden DataViewSetting objekt pro každou tabulku v datové sadě. DataViewSettingCollectionThe DataViewSettingCollection contains one DataViewSetting object for each table in a DataSet. Můžete nastavit výchozí vlastnosti ApplyDefaultSort, Sort, RowFilter vyžaduje hodnotua vlastnost RowStateFilter odkazované tabulky pomocí jejího DataViewSetting.You can set the default ApplyDefaultSort, Sort, RowFilter, and RowStateFilter properties of the referenced table by using its DataViewSetting. Můžete odkazovat na DataViewSetting pro konkrétní tabulku podle názvu nebo ordinálního odkazu nebo předáním odkazu na tento konkrétní objekt tabulky.You can reference the DataViewSetting for a particular table by name or ordinal reference, or by passing a reference to that specific table object. Ke kolekci objektů DataViewSetting můžete přistupovat v objektu DataViewManager pomocí vlastnosti DataViewSettings .You can access the collection of DataViewSetting objects in a DataViewManager by using the DataViewSettings property.

Následující příklad kódu vyplní datovou sadu pomocí SQL Server Northwind databáze tabulky zákazníci, objednávkya Podrobnosti objednávky, vytvoří vztahy mezi tabulkami, používá objekt DataViewManager k Nastavte výchozí nastavení DataView a naváže objekt DataGrid k objektu DataViewManager.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. Příklad Nastaví výchozí nastavení zobrazení pro všechny tabulky v datové sadě pro řazení podle primárního klíče tabulky (ApplyDefaultSort = true) a pak upraví pořadí řazení tabulky Customers na Seřadit podle CompanyName.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");  

Viz také:See also