In C#, when working with DataTable objects, you often need to filter data dynamically without modifying the original table. The DataView class is designed for this purpose. It provides a view of a DataTable and allows you to sort, filter, and search rows efficiently.
Understanding the Scenario
Consider you have a DataTable called dt containing columns like:
SymbolCategoryopendatetimeclosedatetimeFloorPriceCeilingPriceasbanonasba
You want to filter rows where a specific column matches a value.
Using DataView to Filter Rows
else
{
// Create a DataView from the DataTable
DataView dv = dt.DefaultView;
// Apply a filter condition
dv.RowFilter = "asbanonasba = '1'";
// Convert the filtered view back to a DataTable
DataTable dts = dv.ToTable();
}
Step-by-Step Explanation
Create a DataView
DataView dv = dt.DefaultView;
DefaultViewcreates aDataViewobject linked to theDataTable.Changes in the
DataViewdo not modify the original DataTable.
Set the RowFilter
dv.RowFilter = "asbanonasba = '1'";
RowFilteris similar to a SQLWHEREclause.Syntax
"ColumnName operator value"
Examples
dv.RowFilter = "Category = 'IND'"; // Filter by text
dv.RowFilter = "FloorPrice > 100"; // Filter numeric values
dv.RowFilter = "opendatetime >= '2025-10-01'"; // Filter dates
Convert the Filtered View Back to a DataTable
DataTable dts = dv.ToTable();
dv.ToTable()creates a newDataTablecontaining only the rows that satisfy theRowFilter.You can now use
dtsfor binding to UI controls, exporting, or further processing.
Optional: Selecting Specific Columns and Distinct Rows
You can also select only certain columns or remove duplicates using the overloaded ToTable method:
DataTable distinctTable = dv.ToTable(
true, // distinct rows only
"Symbol", "Category", "FloorPrice" // columns to include
);
Practical Example
Suppose your DataTable has the following data:

Join the conversation! Your thoughts help the community grow.