10

Assuming I have a column called A and I want to check if A is null or blank, what is the proper way to check for this using the DataView's RowFilter:

DataTable dt = GetData();

DataView dv = new DataView(dt);

dv.RowFilter = "A IS NOT NULL OR A IS NOT ''";

The above doesn't seem to work though.

entryton
  • 280
  • 5
  • 18
Xaisoft
  • 45,655
  • 87
  • 279
  • 432

3 Answers3

18

Are you tied to .net < 3.5? If not you can use linq to check the state of a column.

Otherwise there is an Isnull(,) function like in T-SQL:

dv.RowFilter = "Isnull(a,'') <> ''";
Oesor
  • 6,632
  • 2
  • 29
  • 56
Will Charczuk
  • 919
  • 1
  • 7
  • 17
8

I am assuming you need to retrieve all the records where the value in column A is neither null nor ''

The correct expr is:

 dv.RowFilter = "A IS NOT NULL AND A <> ''";

And to retrieve the filtered records loop on dv.ToTable() like this:

foreach (DataRow dr in dv.ToTable().Rows)
    Console.WriteLine(dr["A"]);

This should work ... cheers!!

Rashmi Pandit
  • 23,230
  • 17
  • 71
  • 111
5

You can add

dv.RowFilter = "CONVERT(Isnull(a,''), System.String) <> ''"

if a column has data type of number as Isnull(a,'') would return number. Eval of number <> 0 would throw the exception.

Shabbir
  • 441
  • 2
  • 7
  • 17