Travis Su
Travis Su

Reputation: 709

DataTable Rowfilter syntax for rows where a column is null or empty

So I tried to filter some rows out if a column is empty or null.
How do I do that?

It looks like I need some sort of SQL-Like statement.
I want something like:

t.DefaultView.RowFilter = string.Format("[disabilities] IS NOT NULL OR EMPTY");

Upvotes: 3

Views: 7136

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125217

As equivalent to String.IsNullOrEmpty in data table filter expression, you can use either of the following options:

  • dt.DefaultView.RowFilter = "ISNULL(ColumnName,'')=''"
  • dt.DefaultView.RowFilter = "LEN(ISNULL(ColumnName,''))=0"
  • dt.DefaultView.RowFilter = "ColumnName IS NULL OR ColumnName=''"

To make it !String.IsNullOrEmpty, you can use NOT(criteria) or use not equal operator <>:

  • dt.DefaultView.RowFilter = "NOT(ISNULL(ColumnName,'')='')"
  • dt.DefaultView.RowFilter = "NOT(LEN(ISNULL(ColumnName,''))=0)"
  • dt.DefaultView.RowFilter = "NOT(ColumnName IS NULL OR ColumnName='')"

For more information about filter expression syntax, take a look at DataColumn.Expression.

Upvotes: 5

Related Questions