DataView
A DataView is a live, filtered and sorted window onto a DataTable — no extra database query required.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 29, 2026
Related tutorials
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#
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");
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#
- The view shows products of ₹2000 or more, most expensive first.
- Changing RowFilter updates the view immediately.
- 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#
A quote in the input breaks the expression. Escape single quotes (' → '') or filter with LINQ.
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
var changes = new DataView(table) { RowStateFilter = DataViewRowState.ModifiedCurrent };
Mini quiz#
Which property sorts a DataView?
Want more? Practise all ADO.NET MCQs · ADO.NET interview questions
My notes#
Saved in this browser.