Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataGrid WinForm
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 19-Mar-13 10:58am
supernorb2.6K
Comments
Maciej Los at 19-Mar-13 16:05pm
   
Each DataGridVie is "connected" with datasource. What is your datasource?
supernorb at 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 at 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 at 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
good
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%'";
  Permalink  
Comments
supernorb at 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 at 22-Mar-13 2:38am
   
Good answer, my 5!
Rate this: bad
good
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): http://www.microsoft.com/en-us/download/details.aspx?id=23459[^]
 
Is it enough for start?
  Permalink  
Comments
supernorb at 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 at 20-Mar-13 2:54am
   
Developer code smaples: http://code.msdn.microsoft.com/[^]
Rate this: bad
good
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:
 
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:
 
  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.
Thanks!
  Permalink  

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2014
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