The DataView class in ADO.NET provides a way to represent a
customized view of a DataTable. It allows sorting, filtering, and searching within a
DataTable without modifying the original data.
Namespace:
using System.Data;
Key Features of DataView
Filtering: Filter rows based on conditions using the RowFilter property.
Sorting: Sort data using the Sort property.
Searching: Use Find() to locate rows.
Editing: Modify rows without affecting the original DataTable.
Dynamic Updates: Reflects changes made to the DataTable dynamically.
Creating a DataView
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Rows.Add(1, "Alice", 25);
dt.Rows.Add(2, "Bob", 30);
dt.Rows.Add(3, "Charlie", 22);
DataView dv = new DataView(dt);
Sorting in DataView
dv.Sort = "Age ASC"; // Sort by Age in ascending order
Filtering in DataView
dv.RowFilter = "Age > 25"; // Show only rows where Age > 25
Finding Rows in DataView
Before using Find(), the Sort property must be set.
dv.Sort = "ID";
int index = dv.Find(2); // Find row where ID = 2
if (index != -1)
Console.WriteLine("Row found: " + dv[index]["Name"]);
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The
DataViewclass in ADO.NET provides a way to represent a customized view of aDataTable. It allows sorting, filtering, and searching within aDataTablewithout modifying the original data.Namespace:
Key Features of
DataViewRowFilterproperty.Sortproperty.Find()to locate rows.DataTable.DataTabledynamically.Creating a
DataViewSorting in
DataViewFiltering in
DataViewFinding Rows in
DataViewBefore using
Find(), theSortproperty must be set.Adding New Row
When to Use
DataView?DataTable.DataTablefor every filter/sort would be inefficient.GridView, where you want to show a filtered view.