次の方法で共有


DataView での DataRowView コレクションのクエリ

DataViewは、DataRowView オブジェクトの列挙可能なコレクションを公開します。 DataRowView は、 DataRow のカスタマイズされたビューを表し、その DataRow の特定のバージョンをコントロールに表示します。 DataRowなど、コントロールを介して表示できるDataGridViewのバージョンは 1 つだけです。 DataRowDataRowView プロパティを使用して、Rowによって公開されるDataRowViewにアクセスできます。 DataRowViewを使用して値を表示する場合、RowStateFilter プロパティによって、基になるDataRowのどの行バージョンが公開されているかが決まります。 DataRowを使用してさまざまな行バージョンにアクセスする方法については、「行の状態と行のバージョン」を参照してください。 DataRowViewによって公開されるDataView オブジェクトのコレクションは列挙可能であるため、LINQ to DataSet を使用してクエリを実行できます。

次の例では、 Product テーブルに赤い色の製品を照会し、そのクエリからテーブルを作成します。 テーブルから DataView が作成され、削除された行と変更された行をフィルター処理するように RowStateFilter プロパティが設定されます。 その後、 DataView は LINQ クエリのソースとして使用され、変更および削除された DataRowView オブジェクトは DataGridView コントロールにバインドされます。

DataTable products = _dataSet.Tables["Product"];

// Query for red colored products.
EnumerableRowCollection<DataRow> redProductsQuery =
    from product in products.AsEnumerable()
    where product.Field<string>("Color") == "Red"
    orderby product.Field<decimal>("ListPrice")
    select product;

// Create a table and view from the query.
DataTable redProducts = redProductsQuery.CopyToDataTable();
var view = new DataView(redProducts);

// Mark a row as deleted.
redProducts.Rows[0].Delete();

// Modify product price.
redProducts.Rows[1]["ListPrice"] = 20.00;
redProducts.Rows[2]["ListPrice"] = 30.00;

view.RowStateFilter = DataViewRowState.ModifiedCurrent | DataViewRowState.Deleted;

// Query for the modified and deleted rows.
IEnumerable<DataRowView> modifiedDeletedQuery = from DataRowView rowView in view
                                                select rowView;

dataGridView2.DataSource = modifiedDeletedQuery.ToList();
Dim products As DataTable = dataSet.Tables("Product")

' Query for red colored products.
Dim redProductsQuery = _
From product In products.AsEnumerable() _
Where product.Field(Of String)("Color") = "Red" _
Order By product.Field(Of Decimal)("ListPrice") _
Select product
' Create a table and view from the query.
Dim redProducts As DataTable = redProductsQuery.CopyToDataTable()
Dim view As DataView = New DataView(redProducts)

' Mark a row as deleted.
redProducts.Rows(0).Delete()

' Modify product price.
redProducts.Rows(1)("ListPrice") = 20.0
redProducts.Rows(2)("ListPrice") = 30.0

view.RowStateFilter = DataViewRowState.ModifiedCurrent Or DataViewRowState.Deleted

' Query for the modified and deleted rows.
Dim modifiedDeletedQuery = From rowView As DataRowView In view _
                           Select rowView

dataGridView2.DataSource = modifiedDeletedQuery.ToList()

次の例では、 DataGridView コントロールにバインドされたビューから製品のテーブルを作成します。 DataViewは赤い色の製品に対してクエリを実行し、順序付けられた結果はDataGridView コントロールにバインドされます。

// Create a table from the bound view representing a query of
// available products.
var view = (DataView)bindingSource1.DataSource;
DataTable productsTable = view.Table;

// Set RowStateFilter to display the current rows.
view.RowStateFilter = DataViewRowState.CurrentRows;

// Query the DataView for red colored products ordered by list price.
var productQuery = from DataRowView rowView in view
                   where rowView.Row.Field<string>("Color") == "Red"
                   orderby rowView.Row.Field<decimal>("ListPrice")
                   select new
                   {
                       Name = rowView.Row.Field<string>("Name"),
                       Color = rowView.Row.Field<string>("Color"),
                       Price = rowView.Row.Field<decimal>("ListPrice")
                   };

// Bind the query results to another DataGridView.
dataGridView2.DataSource = productQuery.ToList();
' Create a table from the bound view representing a query of
' available products.
Dim view As DataView = CType(bindingSource1.DataSource, DataView)
Dim productsTable As DataTable = CType(view.Table, DataTable)

' Set RowStateFilter to display the current rows.
view.RowStateFilter = DataViewRowState.CurrentRows

' Query the DataView for red colored products ordered by list price.
Dim productQuery = From rowView As DataRowView In view _
                   Where rowView.Row.Field(Of String)("Color") = "Red" _
                   Order By rowView.Row.Field(Of Decimal)("ListPrice") _
                   Select New With {.Name = rowView.Row.Field(Of String)("Name"), _
                                .Color = rowView.Row.Field(Of String)("Color"), _
                                .Price = rowView.Row.Field(Of Decimal)("ListPrice")}

' Bind the query results to another DataGridView.
dataGridView2.DataSource = productQuery.ToList()

こちらも参照ください