Impostazione delle visualizzazioni predefinite delle tabelle mediante DataViewManager

È possibile utilizzare DataViewManager per gestire le impostazioni di visualizzazione per tutte le tabelle di un DataSet. Se si desidera associare un controllo a più tabelle, ad esempio una griglia per l'esplorazione delle relazioni, DataViewManager rappresenta la soluzione ideale.

Nel DataViewManager è contenuto un insieme di oggetti DataViewSetting utilizzati per definire le impostazioni di visualizzazione delle tabelle nel DataSet. In DataViewSettingCollection è contenuto un oggetto DataViewSetting per ogni tabella di un DataSet. È possibile impostare le proprietà predefinite ApplyDefaultSort, Sort, RowFilter e RowStateFilter della tabella a cui si fa riferimento mediante il relativo DataViewSetting. È possibile fare riferimento a DataViewSetting per una particolare tabella per nome o per riferimento ordinale oppure passando un riferimento a tale oggetto tabella specifico. La proprietà DataViewSettings consente di accedere all'insieme di oggetti DataViewSetting in DataViewManager.

Nell'esempio di codice seguente un DataSet viene riempito con le tabelle Customers, Orders e Order Details del database Northwind di Microsoft SQL Server 7.0, vengono create le relazioni tra tabelle, viene utilizzato un DataViewManager per definire le impostazioni predefinite di DataView e un DataGrid viene associato a DataViewManager. L'esempio consente di definire le impostazioni predefinite per DataView per tutte le tabelle nel DataSet, in modo da ordinarle in base alla chiave primaria della tabella (ApplyDefaultSort = true), quindi di modificare l'ordinamento della tabella Customers per applicare l'ordinamento in base a CompanyName.

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

Vedere anche

Creazione e utilizzo di DataView | Classe DataSet | Classe DataViewManager | Classe DataViewSetting | Classe DataViewSettingCollection