Click here to Skip to main content
12,394,971 members (64,772 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# DataGrid
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!


Posted 19-Mar-13 9:58am
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

BindingSource b = (BindingSource)dataGridView1.DataSource;
b.Filter = "FullName like '%xyz%'";
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!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

See this article: Datagridview with filtering capability[^]

I recommend you to download this example application (code):[^]

Is it enough for start?
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:[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:


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

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

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

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100