Question

How can you use the RowFilter to search on a string with an ampersand in (or any other 'special' character. e.g. !"£$%^&*())

When I try I receive the following error:

Error in Like operator: the string pattern '%£(*$\&%' is invalid

A copy of my RowFilter is below:

value="£(*$\&"; //I know this is rubbish, but I don't want the system to crash.

filterString = string.Format("Description LIKE '%{0}%'", value);

myDataView.RowFilter=filterString;

Thanks in advance.

I'm using C# 4.0

Was it helpful?

Solution

May be this one can help..

CheckValue("fefe[][]12#");
CheckValue("abvds");
CheckValue("#");
CheckValue(@"[][][][][]\\\\\][]");
CheckValue("^^^efewfew[[]");

public static string CheckValue(string value)
{
    StringBuilder sBuilder = new StringBuilder(value);

    string pattern = @"([-\]\[<>\?\*\\\""/\|\~\(\)\#/=><+\%&\^\'])";

    Regex expression = new Regex(pattern);

    if (expression.IsMatch(value))
    {
        sBuilder.Replace(@"\", @"\\");
        sBuilder.Replace("]", @"\]");
        sBuilder.Insert(0, "[");
        sBuilder.Append("]");
    }
    return sBuilder.ToString();
}

Also check http://www.csharp-examples.net/dataview-rowfilter/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top