Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check for blank in DataView.RowFilter

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.

like image 208
Xaisoft Avatar asked May 19 '09 17:05

Xaisoft


People also ask

What property filters the data in a DataView?

RowFilter Property (System. Data) Gets or sets the expression used to filter which rows are viewed in the DataView.

Can you use a DataView to filter rows in a data table?

Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression. A DataView provides a dynamic view of data in the underlying DataTable: the content, ordering, and membership reflect changes as they occur.


2 Answers

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,'') <> ''";
like image 52
Will Charczuk Avatar answered Oct 11 '22 13:10

Will Charczuk


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!!

like image 25
SO User Avatar answered Oct 11 '22 12:10

SO User