查詢 DataView 中的 DataRowView 集合
DataView 公開可列舉之 DataRowView 物件的集合。 DataRowView 代表 DataRow 的自訂檢視並會在控制項中顯示該 DataRow 的特定版本。 只有一個 DataRow 的版本能透過控制項顯示,例如 DataGridView。 您可以透過 DataRow 的 DataRowView 屬性,存取 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()