Problem in dataView.RowFilter

Posted on 2009-12-16
Last Modified: 2013-12-17
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 ?????
Question by:alnahas1
    LVL 42

    Expert Comment

    since the Myaddress contans brackets u need to modify the filter a little bit.
    the first section explans how to use special charachters in filter.

    Author Comment

    This is a great website but i afraid it's not help me
    please any ather solve
    thanks in advance
    LVL 14

    Expert Comment

    by:Dhanasekaran Sengodan

     (last sentence is important):


    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.
    LVL 42

    Expert Comment


    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 == ']')
        else if (c == '\'')
      return sb.ToString();


    Author Comment

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

    Expert Comment

    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

    LVL 42

    Accepted Solution


    if i'm not mistaken ur misusing the LIKE clause.
    i assumed that the brackets are part of the string ur trying to filter.
    however, ur logic is 2 use the letters in the brackets to produce multiple options based on the number of the characters inside the brackets.

    so what u mean basically is that if your Myaddress string is: "123[XYZ]123"

    your filter suppose to be look like this:
    dataView.RowFilter = "Columnaddress like '%123X123%'  or
    Columnaddress like '%123Y123%'  or
    Columnaddress like '%123Z123%' ";

    if this what u mean u need to build the expression yourself cause this kind of logic is not supported.

    what i'd do is to write a function which accept column name and expression (i.e. "al[ea]xandria") and return the filter expression with the logic u want to apply.

    check the below code:

    static void Main(string[] args)
                string exp = GenerateOptionsFilter("City", "New[ -]York");
                //City='New York' or City='New-York' 
            static string GenerateOptionsFilter(string column, string input)
                string[] tokens = input.Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
                if (tokens.Count() != 3)
                    //invlid input
                StringBuilder sb = new StringBuilder();
                foreach (char c in tokens[1])
                    sb.AppendFormat("{0}='{1}", column, tokens[0]);
                    sb.Append("' or " );
                string exp = sb.ToString();
                return exp.Substring(0, exp.Length - 3);

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now