开发者

Dataview RowFilter escape invalid characters

开发者 https://www.devze.com 2023-03-14 09:21 出处:网络
Does any one have a handy ready to use method to escape wildcards, invalid characters in the string to be assigned to RowFilter for a DataView in C#. The rules somewhat are like below

Does any one have a handy ready to use method to escape wildcards, invalid characters in the string to be assigned to RowFilter for a DataView in C#. The rules somewhat are like below

If a column name contains any of these special characters ~ ( ) # \ / = > < + - * % & | ^ ' " 

[ ], you must enclose the column name within squar开发者_C百科e brackets [ ]. If a column name contains 

right bracket ] or backslash \, escape it with backslash (\] or \\).

thanks :)


This will work based on the specs outlined in a bit more detail found here

protected void Page_Load(object sender, EventArgs e)
{
    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();
}

I tossed it into a web page I was looking at so disregard the Page_Load and of course test however you would like.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消