DataViews in ADO.NET
DataView
objects provide a way to create a customized view of a DataTable
. This includes sorting, filtering, and navigating the data. They are particularly useful for data-bound controls in Windows Forms or ASP.NET applications.
What is a DataView?
A DataView
is not a new data store. Instead, it is a powerful object that allows you to dynamically sort and filter the rows of a DataTable
without modifying the original table itself. Each DataView
object is associated with a single DataTable
.
You can create multiple DataView
objects for the same DataTable
, each with its own unique sorting and filtering criteria. This makes it very efficient to present the same data in different ways to the end-user.
Key Features of DataView
- Sorting: Define a sort order for the rows based on one or more columns.
- Filtering: Apply filter expressions to display only a subset of rows that meet specific criteria.
- Row Navigation: Move forwards and backwards through the rows of the view.
- Dynamic Updates: Changes made to the underlying
DataTable
are reflected in theDataView
automatically (and vice-versa, with some restrictions). - Data Binding: Easily bind to UI controls like
DataGrid
(Windows Forms) orGridView
(ASP.NET).
Creating and Using a DataView
To use a DataView
, you first need a DataTable
. You can then create a DataView
instance and associate it with the table.
Example: Basic Usage
using System;
using System.Data;
public class DataViewExample
{
public static void Main(string[] args)
{
// 1. Create a DataTable
DataTable customersTable = new DataTable("Customers");
customersTable.Columns.Add("CustomerID", typeof(int));
customersTable.Columns.Add("CompanyName", typeof(string));
customersTable.Columns.Add("City", typeof(string));
customersTable.Rows.Add(1, "Alfreds Futterkiste", "Berlin");
customersTable.Rows.Add(2, "Ana Trujillo Emparedados y helados", "México D.F.");
customersTable.Rows.Add(3, "Around the Horn", "London");
customersTable.Rows.Add(4, "Berglunds snabbköp", "Luleå");
customersTable.Rows.Add(5, "Blauer See Delikatessen", "Mannheim");
// 2. Create a DataView from the DataTable
DataView customerView = new DataView(customersTable);
// 3. Apply sorting
customerView.Sort = "CompanyName ASC";
Console.WriteLine("Customers sorted by Company Name:");
foreach (DataRowView rowView in customerView)
{
Console.WriteLine($" - {rowView["CompanyName"]} ({rowView["City"]})");
}
Console.WriteLine("\n---------------------------\n");
// 4. Apply filtering
customerView.RowFilter = "City = 'London'";
customerView.Sort = "CustomerID DESC"; // Sorting can be changed
Console.WriteLine("Customers from London (sorted by CustomerID DESC):");
foreach (DataRowView rowView in customerView)
{
Console.WriteLine($" - {rowView["CompanyName"]} ({rowView["City"]})");
}
}
}
Sorting
The Sort
property of a DataView
accepts a string that specifies the column(s) to sort by and the order (ASC
for ascending, DESC
for descending). Multiple columns can be specified, separated by commas.
Example: customerView.Sort = "City DESC, CompanyName ASC";
Filtering
The RowFilter
property accepts a string expression that specifies the criteria for filtering rows. The syntax is similar to the WHERE
clause in SQL.
Common operators: =, <, >, <=, >=, <>, LIKE, IN
.
String values should be enclosed in single quotes ('
). Numeric values do not need quotes.
Example: customerView.RowFilter = "CustomerID > 100 AND City LIKE 'A%'";
DataViewManager
and its DataView.RowFilter
property which supports a richer syntax.
DataRowView
When you iterate through a DataView
, you get DataRowView
objects. These objects provide access to the data in a particular row of the view. You can access columns by name or by index.
DataRowView currentRow = (DataRowView)customerView[0]; // Get the first row in the view
string companyName = currentRow["CompanyName"].ToString();
Common Scenarios
- Displaying data in a grid with sortable columns.
- Creating master-detail relationships where a selection in one grid filters another.
- Implementing search functionalities that filter data based on user input.