Filtering Dataviews

G

Guest

The DataView RowFilter property rather annoyingly doesn't allow for wildcards
in the middle of strings. So, for example:

col LIKE 'A*B'

doesn't work, i.e. it doesn't return all rows where col starts with A and
ends with B and has anything inbetween. To get around this I derived my own
class from DataView and parsed the RowFilter string like this:

// Look for wildcards that DO NOT occur at the start or end of the string
Regex reg = new Regex(@"(?<=.)\*(?=.)");
string strReplace = "*' AND " + col + " LIKE '*";
RowFilter = reg.Replace(strWild,strReplace);

so

col LIKE 'A*B'

becomes

col LIKE 'A*' AND col LIKE '*B'

this works quite well except in this case:

col LIKE '*A*B*'

which becomes:

col LIKE '*A*' AND col LIKE '*B*'

this will work, it'll give you rows that contain and A followed by a B
anywhere in the string, but it also returns strings that contain B followed
by A. For example, given these rows:

xxxAxxxBxxx
xxABxxxxxxx
xxxBxxxAxxx
xxxAxxxxxxx

The first 3 rows would be returned, but row 3 should not have been returned
since A and B are in the wrong order. Does anybody have any ideas on how I
could handle this? Or is there an easier way to deal with wildcards? And why
doesn't DataView support wildcards in the middle of strings in the first
place?

Thanks
 
G

Guest

Unfortunately that doesn't work. The RowFilter property of the dataview does
not allow wildcards in the middle of a string, only at the begining or the
end. This is the problem I am trying to work around.
 
B

Bernie Yaeger

Hi,

Try using a dataview.select instead. That syntax should allow you to use
like as you wish.

HTH,

Bernie Yaeger
 
G

Guest

Thanks for the reply.
I assume you mean DataTable.Select? Unfortunately that using the same rules
for the filter expression as DataView.RowFilter, i.e. the rules for
DataColumn's Expression property, and suffers with the same limitation, no
wild cards in the middle of strings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top