Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know that we can use some property like BindingSource.Filter to filter for wanted rows, however I don't know how to filter for rows by word (not by the whole text of cells), for example, my datagridview has a column called Full name, the texts in that column can be "A xyz", "B xyz", "C xyz" ... , how can I filter for all rows which have xyz as the last word, the filter should be applied right when user types in a name, for example, xyz = "nice name", all the rows with the last word (in the column Full name) starting with 'n' (after 'n' is typed in, 'ni' after 'ni' is typed in, 'nic' after 'nic' is typed in,...) should be returned. Because of that requirement (filter right when typing in), the pattern "[Full name] like '% xyz'" won't work exactly as I want.

I can't use full SQL syntax for Filter property, the very useful SQL functions for this such as charindex, reverse, ... are not supported.

Your help would be highly appreciated!

Thanks.

VipHaLong
Posted
Comments
Maciej Los 19-Mar-13 16:05pm    
Each DataGridVie is "connected" with datasource. What is your datasource?
supernorb 19-Mar-13 17:57pm    
My datasource can be any but it's assigned for DataGridView through BindingSource, and BindingSource has a property called Filter.
Maciej Los 20-Mar-13 3:02am    
Please, never reply like that, unless you don't need help. It's just like in joke:
- What do you want to drink?
- Anything...
- Let me bring you a water from loo, OK?


Finally, what is your datasource?
supernorb 21-Mar-13 18:47pm    
I don't think the datasource can matter anything here, I just created a table with some columns in a SQL server database, connected to the database and filled the table in a dataset using adapter, the underlying datasource is a DataSet, however to filter data I created a BindingSource and assigned its DataSource to the dataset and its DataMember to a table name in the dataset. The direct datasource here is a BindingSource, maybe I misunderstood what datasource you wanted to mean, because I'm interacting with a BindingSource, I don't think the kind of the underlying datasource can matter (any), That's why the term 'class' in programming is very interesting, If you think of another approach for filtering without using Filter property of BindingSource, please state it clearly before asking the question. Thanks.

You can use "Like" and "%" in filter criteria as given below.

C#
BindingSource b = (BindingSource)dataGridView1.DataSource;
b.Filter = "FullName like '%xyz%'";
 
Share this answer
 
Comments
supernorb 21-Mar-13 18:58pm    
Your filter string should be modified as "Fullname like '% xyz %'", it's for a word, not any string, however this will work only when you already have the whole word input from user. The case I want here is filter for the last word only and the string will be "Fullname like '% xyz'", this is also mentioned in my question and it can't help. If we have the whole word xyz, it does work, however if user types in the first character of the last word, the filter will filter for all rows with the words having that first character as the last character, please read my original question carefully again. Thanks.
Maciej Los 22-Mar-13 2:38am    
Good answer, my 5!
See this article: Datagridview with filtering capability[^]

I recommend you to download this example application (code): http://www.microsoft.com/en-us/download/details.aspx?id=23459[^]

Is it enough for start?
 
Share this answer
 
Comments
supernorb 19-Mar-13 17:55pm    
I'm sorry but are you sure the code in those samples has anything for me to refer to achieve what I want? I've tried running the samples but they filter another way (the whole text), I haven't looked deeply in code, if possible please give me an exact solution, I don't think it can't be briefed in some lines of code. By the way, could you please tell me how to find source code on Microsoft? (like as the link you suggested), I would like to know some category or zone in Microsoft for source/sample code only (dedicatedly not mixed with other contents and can only be found by searching). Thanks!
Maciej Los 20-Mar-13 2:54am    
Developer code smaples: http://code.msdn.microsoft.com/[^]
It's likely that there isn't a better solution, I have a workaround here and this should be involved to SQL SELECT statement. First I have to change the SELECT statement to include a hidden column, this column will contain only the last word of the visible column and this column is invisible when filling data in DataGridView. The filter will apply on this hidden column instead of the visible column.

Here are details:

SQL
SELECT REVERSE(RTRIM(SUBSTRING(REVERSE(Fullname),1,CHARINDEX(' ',REVERSE(Fullname))))) [Name] FROM MyTable


Suppose I have a DataGridView named gridView, then after assigning its datasource, we make the column 'Name' invisible:

C#
gridView.Columns["Name"].Visible = false;


And the last, we apply the Filter on the hidden column (Name):

C#
myBindingSource.Filter = "[Name] like 'variableText%'";


The line of code above will be placed in some TextChanged event handler, user will change the text by typing in some textbox and the Filter will change accordingly. This works great however it's not a direct solution, it's a workaround, I would like someone to give me the direct solution.
Thanks!
 
Share this answer
 

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