Monday, 13 October 2025

Using DataView in C# to Filter Rows in a DataTable

Leave a Comment

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.

 
Understanding the Scenario

Consider you have a DataTable called dt containing columns like:

  • Symbol

  • Category

  • opendatetime

  • closedatetime

  • FloorPrice

  • CeilingPrice

  • asbanonasba

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
  1. Create a DataView

DataView dv = dt.DefaultView;
  • DefaultView creates a DataView object linked to the DataTable.

  • Changes in the DataView do not modify the original DataTable.

  1. Set the RowFilter

dv.RowFilter = "asbanonasba = '1'";
  • RowFilter is similar to a SQL WHERE clause.

  • 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
  1. Convert the Filtered View Back to a DataTable

DataTable dts = dv.ToTable();
  • dv.ToTable() creates a new DataTable containing only the rows that satisfy the RowFilter.

  • You can now use dts for 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:

SymbolCategoryasbanonasba
ABCIND1
DEFIND0
XYZFMCG1

Filtering with

dv.RowFilter = "asbanonasba = '1'";

The resulting DataTable will contain:

SymbolCategoryasbanonasba
ABCIND1
XYZFMCG1

Tips and Best Practices
  1. Always check for column names in the filter string — typos will throw exceptions.

  2. String values must be enclosed in single quotes ' '.

  3. DateTime values must be in MM/dd/yyyy format or use # in US format:

dv.RowFilter = "opendatetime >= #10/01/2025#";
  1. For numeric columns, quotes are not needed:

dv.RowFilter = "CeilingPrice >= 500";
  1. DataView vs LINQ: DataView is faster for UI-bound tables; LINQ to DataSet is more flexible for complex queries.

Conclusion

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

One of the most important things when choosing a good ASP.NET Core 9.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

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