Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi All,

I am using data filter from DataTable using Select method.In my case Column Header have a special character so in filter section comes error.

Here is my Code...

In this case its working fine..
string sCondition = string.Format("[{0}] = '{1}'", "Country Name", "India");
DataRow[ ] dr = dtData.Select(sCondition);


Problem is occured in this case...
string sCondition = string.Format("[{0}] = '{1}'", "Unit [in Cr]", "INR");
DataRow[ ] dr = dtData.Select(sCondition);


In this case Column Name have brace '[ ] ' so my filter condition is not working..
Exception thrown: "Cannot interpret token ']' at position 11"
Please any one suggest me how to filter this type of column using Select method.

Advance Thanks.... :(
Posted
Updated 30-Nov-10 1:55am
v2
Comments
Hiren solanki 30-Nov-10 8:31am    
you need to rename a column name to overcome this.
T.Saravanann 30-Nov-10 8:58am    
is not possible because column name come from database so can not rename the column..but i found the solution.. see below answer..

1 solution

Hi All,

Thanks Friends...

Now i am found out the solution..
Just add a slash ("\") in near to closing brace...

string sCondition = string.Format("[{0}] = '{1}'", "Unit [in Cr\]", "INR");
DataRow[ ] dr = dtData.Select(sCondition);


Reference Link: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression%28VS.71%29.aspx


Cheers :)
 
Share this answer
 
Comments
Toli Cuturicu 30-Nov-10 11:20am    
Yes, this is called an escape sequence. I really thought Hiren Solanki knew that!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900