Querying the DataRowView Collection in a DataView

The DataView exposes an enumerable collection of DataRowView objects. DataRowView represents a customized view of a DataRow and displays a specific version of that DataRow in a control. Only one version of a DataRow can be displayed through a control, such as a DataGridView. You can access the DataRow that is exposed by the DataRowView through the Row property of the DataRowView. When you view values by using a DataRowView, the RowStateFilter property determines which row version of the underlying DataRow is exposed. For information about accessing different row versions using a DataRow, see Row States and Row Versions. Because the collection of DataRowView objects exposed by the DataView is enumerable, you can use LINQ to DataSet to query over it.

The following example queries the Product table for red-colored products and creates a table from that query. A DataView is created from the table and the RowStateFilter property is set to filter on deleted and modified rows. The DataView is then used as a source in a LINQ query, and the DataRowView objects that have been modified and deleted are bound to a DataGridView control.


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<DataRow>();
DataView 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();


The following example creates a table of products from a view that is bound to a DataGridView control. The DataView is queried for red-colored products and the ordered results are bound to a DataGridView control.


// Create a table from the bound view representing a query of 
// available products.
DataView view = (DataView)bindingSource1.DataSource;
DataTable productsTable = (DataTable)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();


Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft