Dataview row Filter issue
When we use something like this to filter the dataview we get some error.....what could be the reason...?
filter=" [FieldName] LIKE 'adfasdfsd*asdfasdfasd%' ";
dataview.RowFilter= filter;
Could anyone explain the reason why * char is treat in special manner...... it act as a wild character if we simply do
filter=" [FieldName] LIKE '*%' ";
dataview.RowFilter= filter;
it doesnot display entry starting from * , rather it display all the data without any filter applied.
Eagerly waiting for the reason.....
Thanks in advance...
What is the exact error message?
To filter:
BindingSource theBindingSource = new BindingSource();
theBindingSource.DataSource = this.theDataGridView.DataSource; (or dataTable.defaultview)
theBindingSource.Filter = "[columnName] = 'query'";
http://msdn2.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter.aspx
or perhaps:
//datatable
theDataTable.DefaultView.RowFilter = "[columnName] = 'query'";
* in SQL means everything, therefore it will show everything in your filter query.
have you tried placing the * in square brackets? :
Thanks ahmedilyas..
It displays the error that the pattern "asdfas*asdfasdf%" is invalid.... Later i identified that its the * character that the rowfilter query takes special care of. I have posted my finding in the previous post. please take a look into that and please could you explain the issue to me....
Ok got that * meaning.......... however why is it displaying a pattern invalid error? and is there any method so that i can make search for those string that contains * character. ok thanks a lot ......it does work placing the * in the [] (Square bracket).. incredible!!!!!!!
How to find the string that contains % character within it?
But to ask you a question is there other pattern or character that could generate the pattern error.....?
try this:
"[columnName] = '
%'";so its a square opening bracket, star, close square bracket and finally the percent sign
it shouldnt...
these special characters can be "escaped" from their meaning by placing the square brackets around them. This should solve the problems and is best used at all times to prevent such things from occuring
I finally found the answer to my query in MSDN ......here is the answer to my query....However now i arrived to an dead end due to the issue of '[' and ']'.
WILDCARD CHARACTERS
Both the * and % can be used interchangeably for wildcard characters 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 start and end of a pattern, or at the end of a pattern, or at the start of a pattern. For example:
"ItemName LIKE '*product*'"
"ItemName LIKE '*product'"
"ItemName LIKE 'product*'"
Wildcard characters are not allowed in the middle of a string. For example, 'te*xt' is not allowed.
What should i do to filter the string containing "[]"? As per the above solution, i can escape the '[' bracket with '[]]' and now i have to replace ']' character...But as our original string has now changed to '[]]]' and if i replace ']' with '[]]' then i am just in a dead end....Which generates the pattern error.....!!!!!!!!!!!!!!!!
Could anyone find a simple solution to this ?
you are looking for a LIKE statement.
so an example:
ItemName LIKE '%product%'
would bring back any "itemName" which has the word product in it
if you wanted to bring the records back containing the word "product" and something after,
ItemName LIKE 'product%'
would bring back everything with "product" and "production"
Thanks for your generous effort and time you have taken to write in a response.. however now my question is something different which is given as below..
What should i do to filter the string containing "[]"? As per the above solution, i can escape the '[' bracket with '[]]' and now i have to replace ']' character...But as our original string has now changed to '[]]]' and if i replace ']' with '[]]' then i am just in a dead end....Which generates the pattern error.....!!!!!!!!!!!!!!!!
Could anyone find a simple solution to this ?
You could do a dummy-replacement first to a char which is not included in the string. See my code below:
string chr1 = null;string chr2 = null;string str = "test[]test2";for (int k = 0; k < 256; k++){ if (str.IndexOf((char)k) < 0) { chr2 = chr1; chr1 = new string((char)k, 1); if (chr2 != null) { break; } }}if (chr2 != null){ string strn = str.Replace("[", chr1).Replace("]", chr2).Replace(chr1, "[[]").Replace(chr2, "[]]"); MessageBox.Show(str + " -> " + strn);}else{ MessageBox.Show("Could not replace [ and ]");}There are very few cases, where this code does not work. The user must enter all available minus 1 chars so this code doesn't find a char which fits.
Thanks likely to work if the user didnot enter 256 char in a single string..