MSDN Documentation

DataView

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.

Introduction

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.

Key Features

Usage

To use a DataView, you typically follow these steps:

  1. Create a DataTable and populate it with data.
  2. Create a new DataView object, passing the DataTable to its constructor.
  3. (Optional) Set the Sort property to define the sorting order.
  4. (Optional) Set the RowFilter property to define the filtering criteria.
  5. (Optional) Use the Find or FindRows methods to locate specific data.
  6. Bind the DataView to your UI control.

Sort Property

The 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 Property

The 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#.

Code Examples

Creating and Filtering a DataView


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"]}");
}
        

Finding Rows

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.

Performance Considerations

See Also