Link to home
Start Free TrialLog in
Avatar of alnahas1
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 ?????
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

since the Myaddress contans brackets u need to modify the filter a little bit.
chcek http://www.csharp-examples.net/dataview-rowfilter/
the first section explans how to use special charachters in filter.
Avatar of alnahas1
alnahas1

ASKER

This is a great website but i afraid it's not help me
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.
try:

dataView.RowFilter = "Columnaddress like '%" + EscapeLikeValue(Myaddress) + "%' ";

public static string EscapeLikeValue(string valueWithoutWildcards)
{
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < valueWithoutWildcards.Length; i++)
  {
    char c = valueWithoutWildcards[i];
    if (c == '*' || c == '%' || c == '[' || c == ']')
      sb.Append("[").Append(c).Append("]");
    else if (c == '\'')
      sb.Append("''");
    else
      sb.Append(c);
  }
  return sb.ToString();
}

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial