在 DataView 中查询 DataRowView 集合

DataView 公开了 DataRowView 对象的可枚举集合。 DataRowView 表示 DataRow 的自定义视图,并在控件中显示特定版本的 DataRow。 只有一种版本的 DataRow 可通过控件显示,例如 DataGridView。 您可以通过 DataRowDataRowView 属性访问 Row 公开的 DataRowView。 当使用 DataRowView 查看值时,RowStateFilter 属性决定公开基础 DataRow 的哪个行版本。 有关使用 DataRow 访问不同行版本的信息,请参阅行状态和行版本。 因为 DataView 公开的 DataRowView 对象集合是可枚举的,所以你可以使用 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()

请参阅