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