次の方法で共有


DataViewManager を使用した既定のテーブル ビューの設定

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

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

Microsoft SQL Server 7.0 Northwind データベースに CustomersOrdersOrder Details の各テーブルを格納し、テーブル間のリレーションシップを作成し、DataViewManager を使用して既定の DataView 設定を設定し、DataGridDataViewManager に連結するコード例を次に示します。このコード例では DataSet のすべてのテーブルを対象とした既定の DataView 設定が設定されます。この設定では、テーブルの主キーによってテーブルの内容が並べ替えられ (ApplyDefaultSort = true)、次に Customers テーブルの並べ替え順序が、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");

参照

DataView の作成と使用 | DataSet クラス | DataViewManager クラス | DataViewSetting クラス | DataViewSettingCollection クラス