Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataView row Filtering

Tags:

c#

wpf

c#-4.0

I have two DataView that I am trying to sort, in dgtest1 I am trying to for the exception of the data that contains a Typeid != 25 and in dgtest2 I am trying to only show the data where Typeid == 25.

When I step through the code I am throwing an error on saying

"Cannot interpret token '!' at position 6".

Can someone show me how to properly use the string row filter?

The parameters are (Data Table table, string RowFilter, string Sort, DataViewRowState)

dgtest1.ItemsSource = new DataView(dttest1, "Typeid!= 25", "", DataViewRowState.CurrentRows);
dgtest2.ItemsSource = new DataView(dttest1, "Typeid == 25", "", DataViewRowState.CurrentRows);
like image 409
Robert Avatar asked Sep 03 '13 19:09

Robert


1 Answers

The correct syntax to use for the RowFilter expression in the first Dataview constructor is

dgtest1.ItemsSource = new DataView(dttest1, "Typeid <> 25", "", DataViewRowState.CurrentRows);
                                                    ^^ 

in the second one you need to use

dgtest2.ItemsSource = new DataView(dttest1, "Typeid = 25", "", DataViewRowState.CurrentRows);

The syntax used for the RowFilter parameter in the DataView constructor is the same used for the property Expression of the DataColumn and it is not like the equality operator of C#

EDIT Following your comment below. If Typeid is a database field of text datatype then you need to enclose the value used in the RowFilter between single quotes

dgtest1.ItemsSource = new DataView(dttest1, "Typeid <> '25'", "", DataViewRowState.CurrentRows);
dgtest2.ItemsSource = new DataView(dttest1, "Typeid = '25'", "", DataViewRowState.CurrentRows);

However this seems a little strange. If the Typeid field contains numbers it should be defined as a numeric datatype.

like image 191
Steve Avatar answered Sep 30 '22 15:09

Steve