The DataView
class in ADO.NET provides a dynamic view of a DataTable
that can be filtered, sorted, and searched. It allows you to bind data to controls that support data binding, such as Windows Forms or ASP.NET controls, and to manipulate the data in real-time without affecting the original DataTable
.
A DataView
is not a separate data store; it is a live, updatable view of a DataTable
. Changes made to the underlying DataTable
are automatically reflected in the DataView
, and vice-versa (with some limitations regarding row states). This makes DataView
a powerful tool for presenting and interacting with data.
DataTable
based on specified criteria using a filter expression.DataTable
by one or more columns, in ascending or descending order.DataView
provides methods for finding specific rows that match a given row state and filter criteria.DataView
to UI controls for display and interaction.DataView
can expose rows based on their current state (e.g., Added
, ModifiedCurrent
, Deleted
).To use a DataView
, you typically follow these steps:
DataTable
and populate it with data.DataView
object, passing the DataTable
to its constructor.Sort
property to define the sorting order.RowFilter
property to define the filtering criteria.Find
or FindRows
methods to locate specific data.DataView
to your UI control.Sort
PropertyThe Sort
property accepts a string expression that specifies the column(s) to sort by. For example:
myView.Sort = "LastName ASC, FirstName DESC";
If no sort expression is provided, the default order of the DataTable
is used.
RowFilter
PropertyThe RowFilter
property accepts a string expression that specifies the filtering criteria. This expression language is similar to the one used in SQL WHERE clauses. For example:
myView.RowFilter = "Country = 'USA' AND Age > 30";
Common operators include =
, <>
, >
, <
, >=
, <=
, LIKE
, and IN
.
When filtering strings, enclose them in single quotes (e.g., 'California'
). For numeric values, no quotes are needed. For date/time values, use the format #yyyy-MM-dd#
or #yyyy-MM-dd HH:mm:ss#
.
using System;
using System.Data;
// Assume dt is a populated DataTable
DataView dv = new DataView(dt);
// Set sorting
dv.Sort = "ProductName ASC";
// Set filtering
dv.RowFilter = "UnitPrice > 50 AND Category = 'Electronics'";
// Now dv contains only the filtered and sorted data
// Example: Iterating through the filtered data
foreach (DataRowView rowView in dv)
{
Console.WriteLine($"Product: {rowView["ProductName"]}, Price: {rowView["UnitPrice"]}");
}
The Find
method is used to locate a single row that matches the specified key values and the current filter and sort criteria. It returns the index of the row in the DataView
, or -1 if not found.
// Assuming dv is sorted by a unique key column (e.g., ProductID)
string productIDToFind = "P101";
int rowIndex = dv.Find(productIDToFind);
if (rowIndex != -1)
{
DataRowView foundRow = dv[rowIndex];
Console.WriteLine($"Found Product: {foundRow["ProductName"]}");
}
else
{
Console.WriteLine("Product not found.");
}
The FindRows
method returns an array of row indices for all rows that match the specified key values.
DataView
objects.Sort
and RowFilter
have appropriate indexes defined in the DataTable
to improve lookup performance.