Working with DataTable objects in C# frequently requires dynamic data filtering without changing the underlying table. This is why the DataView class was created. You can effectively sort, filter, and search rows with its view of a data table.
Consider you have a DataTable called dt containing columns like:
SymbolCategoryopendatetimeclosedatetimeFloorPriceCeilingPriceasbanonasba
You want to filter rows where a specific column matches a value.
Create a DataView
DefaultViewcreates aDataViewobject linked to theDataTable.Changes in the
DataViewdo not modify the original DataTable.
Set the RowFilter
RowFilteris similar to a SQLWHEREclause.Syntax
Examples
Convert the Filtered View Back to a DataTable
dv.ToTable()creates a newDataTablecontaining only the rows that satisfy theRowFilter.You can now use
dtsfor binding to UI controls, exporting, or further processing.
You can also select only certain columns or remove duplicates using the overloaded ToTable method:
Suppose your DataTable has the following data:
| Symbol | Category | asbanonasba |
|---|---|---|
| ABC | IND | 1 |
| DEF | IND | 0 |
| XYZ | FMCG | 1 |
Filtering with
The resulting DataTable will contain:
| Symbol | Category | asbanonasba |
|---|---|---|
| ABC | IND | 1 |
| XYZ | FMCG | 1 |
Tips and Best Practices
Always check for column names in the filter string — typos will throw exceptions.
String values must be enclosed in single quotes
' '.DateTime values must be in
MM/dd/yyyyformat or use#in US format:
For numeric columns, quotes are not needed:
DataView vs LINQ:
DataViewis faster for UI-bound tables; LINQ to DataSet is more flexible for complex queries.
Using DataView and its RowFilter property is a simple and effective way to filter rows in a DataTable
without modifying the original data. It’s particularly useful for
binding filtered data to grids, reports, or exporting results.
ASP.NET Core 10.0 Hosting Recommendation
At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.



0 comments:
Post a Comment