alnahas1
asked on
Problem in dataView.RowFilter
I work on vs 2005 c#
Have dataview and I made rowfilte
string Myaddress="alexandria";
Just like dataView1.RowFilter = "Columnaddress like '%" + Myaddress + "%' ";
And its work fine
Problem when I when to make operator or for more than one letter
string Myaddress="al[ea]xandria";
Just like dataView.RowFilter = "Columnaddress like '%" + Myaddress + "%' ";
Error >> Error in Like operator: the string pattern '%[ea]%' is invalid.
I want to make operator or in letters like sql server .
Whats is the slove ?????
Have dataview and I made rowfilte
string Myaddress="alexandria";
Just like dataView1.RowFilter = "Columnaddress like '%" + Myaddress + "%' ";
And its work fine
Problem when I when to make operator or for more than one letter
string Myaddress="al[ea]xandria";
Just like dataView.RowFilter = "Columnaddress like '%" + Myaddress + "%' ";
Error >> Error in Like operator: the string pattern '%[ea]%' is invalid.
I want to make operator or in letters like sql server .
Whats is the slove ?????
ASKER
This is a great website but i afraid it's not help me
please any ather solve
thanks in advance
please any ather solve
thanks in advance
From
http://msdn2.microsoft.com/en-us/library/system.data.datacolumn.expression(vs.71).aspx
(last sentence is important):
WILDCARD CHARACTERS
Both the * and % can be used interchangeably for wildcards in a LIKE comparison. If the string in a LIKE clause contains a * or %, those characters should be escaped in brackets ([]). If a bracket is in the clause, the bracket characters should be escaped in brackets (for example [[] or []]). A wildcard is allowed at the beginning and end of a pattern, or at the end of a pattern, or at the beginning of a pattern. For example:
"ItemName LIKE '*product*'"
"ItemName LIKE '*product'"
"ItemName LIKE 'product*'"
Wildcards are not allowed in the middle of a string. For example, 'te*xt' is not allowed.
http://msdn2.microsoft.com/en-us/library/system.data.datacolumn.expression(vs.71).aspx
(last sentence is important):
WILDCARD CHARACTERS
Both the * and % can be used interchangeably for wildcards in a LIKE comparison. If the string in a LIKE clause contains a * or %, those characters should be escaped in brackets ([]). If a bracket is in the clause, the bracket characters should be escaped in brackets (for example [[] or []]). A wildcard is allowed at the beginning and end of a pattern, or at the end of a pattern, or at the beginning of a pattern. For example:
"ItemName LIKE '*product*'"
"ItemName LIKE '*product'"
"ItemName LIKE 'product*'"
Wildcards are not allowed in the middle of a string. For example, 'te*xt' is not allowed.
try:
dataView.RowFilter = "Columnaddress like '%" + EscapeLikeValue(Myaddress) + "%' ";
public static string EscapeLikeValue(string valueWithoutWildcards)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < valueWithoutWildcards.Leng th; i++)
{
char c = valueWithoutWildcards[i];
if (c == '*' || c == '%' || c == '[' || c == ']')
sb.Append("[").Append(c).A ppend("]") ;
else if (c == '\'')
sb.Append("''");
else
sb.Append(c);
}
return sb.ToString();
}
dataView.RowFilter = "Columnaddress like '%" + EscapeLikeValue(Myaddress)
public static string EscapeLikeValue(string valueWithoutWildcards)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < valueWithoutWildcards.Leng
{
char c = valueWithoutWildcards[i];
if (c == '*' || c == '%' || c == '[' || c == ']')
sb.Append("[").Append(c).A
else if (c == '\'')
sb.Append("''");
else
sb.Append(c);
}
return sb.ToString();
}
ASKER
Mr sedgwick:
this slove no appear error and also make slove to my problem
i mean al[ae]xandria is convert to alxandria
i want dataview filter search for alexandria and alaxandria
this slove no appear error and also make slove to my problem
i mean al[ae]xandria is convert to alxandria
i want dataview filter search for alexandria and alaxandria
If a pattern in a LIKE clause contains any of these special characters * % [ ], those characters must be escaped in brackets [ ] like this [*], [%], [[] or []].
string Myaddress="al[[]ea[]]xandria";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
chcek http://www.csharp-examples.net/dataview-rowfilter/
the first section explans how to use special charachters in filter.