Dataview RowFilter escape invalid characters
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.
精彩评论