ADO.NET · Disconnected Data

DataView

A DataView is a live, filtered and sorted window onto a DataTable — no extra database query required.

Intermediate 5 min Lesson 17 of 32Examples use Microsoft.Data.SqlClient on .NET 9 with SQL Server 2022.
Before you start:SqlDataAdapter
0/32 in ADO.NET
On this page · 5 min · Intermediate

In simple words#

A DataView is a coloured filter sheet over your photocopy: show only some rows, in a chosen order, without changing the copy.

RowFilter
Filter rows in memory.
Sort
Order rows in memory.
ToTable
Copy the visible rows to a new table.

Learning objectives#

  • Filter rows with RowFilter
  • Sort with Sort
  • Find rows by the sort key
  • Create a new table from a view

Explanation#

RowFilter uses a simple expression language: "Price > 1000 AND Name LIKE '%o%'". String values go in single quotes; dates in #…#.

Sort takes column names with ASC or DESC, e.g. "Price DESC, Name".

RowStateFilter shows rows by state — useful for “show only unsaved changes”.

ToTable copies the visible rows (optionally only some columns, optionally distinct) into a new DataTable.

Code example: Filtering and sorting in memory#

C#DataViewDemo.cs Illustrative
var view = new DataView(table)
{
    RowFilter = "Price >= 2000",
    Sort = "Price DESC",
};
foreach (DataRowView row in view)
    Console.WriteLine(
quot;
{row["Name"]}: ₹{row["Price"]}");
view.RowFilter = "Name LIKE 'M%'"; Console.WriteLine(
quot;Starting with M:
{view.Count}");
DataTable names = view.ToTable(distinct: true, "Name"); Console.WriteLine(
quot;Copied
{names.Rows.Count} name(s) into a new table");
Expected output
Laptop: ₹55000.00
Keyboard: ₹2200.00
Starting with M: 1
Copied 1 name(s) into a new table

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. The view shows products of ₹2000 or more, most expensive first.
  2. Changing RowFilter updates the view immediately.
  3. ToTable copies only the Name column of visible rows.

Real-world use case#

A WinForms or WPF grid bound to a DataView filters instantly as the user types in a search box.

Common mistakes#

Building RowFilter from raw input

A quote in the input breaks the expression. Escape single quotes (' → '') or filter with LINQ.

Expecting the database to change

A DataView filters the in-memory copy only.

Best practices#

  • Use DataView for UI filtering of small tables
  • Escape quotes in filter values
  • Use LINQ to DataSet for complex logic
  • Filter large data in SQL instead

Remember this#

DataView = filtered + sorted window on a DataTable.

RowFilter strings use single quotes.

ToTable = copy what you see.

Practice exercise#

Your task: Show only modified rows in a view.

Show solution
C# Illustrative
var changes = new DataView(table) { RowStateFilter = DataViewRowState.ModifiedCurrent };

Mini quiz#

DataView quizMultiple choice Question 1 of 1

Which property sorts a DataView?

Score so far: 0/0

Want more? Practise all ADO.NET MCQs · ADO.NET interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.