次の方法で共有


DataViews の管理 (ADO.NET)

DataView のすべてのテーブルのビュー設定を管理するには、DataViewManager を使用します。 リレーションシップを移動するグリッドなどのように、1 つのコントロールを複数のテーブルに連結する場合は、DataViewManager が適しています。

DataViewManager には、DataSet のテーブルのビュー設定に使用される DataViewSetting オブジェクトのコレクションが含まれています。 DataViewSettingCollection には、DataSet の各テーブルに対応する DataViewSetting オブジェクトが 1 つずつ含まれています。 参照テーブルの既定の ApplyDefaultSortSortRowFilterRowStateFilter の各プロパティを設定するには、DataViewSetting を使用します。 名前または序数参照によって、または参照をその特定のテーブル オブジェクトに渡すことによって、特定のテーブルの DataViewSetting を参照できます。 DataViewManagerDataViewSetting オブジェクトのコレクションにアクセスするには、DataViewSettings プロパティを使用します。

DataSet に、SQL Server Northwind データベースの CustomersOrdersOrder Details の各テーブルを格納し、テーブル間のリレーションシップを作成し、DataViewManager を使用して既定の DataView 設定を指定し、DataGridDataViewManager に連結するコード サンプルを次に示します。 この例では 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)