Click here to Skip to main content
14,976,689 members
Articles / Programming Languages / C#
Article
Posted 2 Jul 2006

Stats

45.8K views
1.5K downloads
29 bookmarked

Support filtering and searching on multiple columns with RowFilterBuilder

Rate me:
Please Sign up or sign in to vote.
2.35/5 (13 votes)
2 Jul 2006CPOL
A class that builds a RowFilter for DataViews to support filtering on multiple columns.

Sample Image

Introduction

If working with a DataGridView or a DataView, you may have been confronted with the problem that you want to supply an easy to use filter (or search) mechanism. In my opinion, "easy to use", in this case, means that the user doesn't have to think about what columns he wants to filter - he just needs to type in the words he is looking for. In the screenshot above, it means that the user is looking for an "Owner" in "Mexico".

Coding this scenario can be very bizarre - that's the reason why I wrote my own class that handles the logic. All you need is to pass the result of a function to the RowFilter property of a DataView.

C#
//example
string filter = Codeproject.RowFilterBuilder.BuildMultiColumnFilter(
                this.textBox1.Text, dataView1);
dataView1.RowFilter = filter;

In the scenario above, dataView1.RowFilter would now be something like this:

"( (CONVERT( [CustomerID], 'System.String') like '%Mexico%' ) OR  
  (CONVERT( [CompanyName], 'System.String') like '%Mexico%' ) OR  
  (CONVERT( [ContactName], 'System.String') like '%Mexico%' ) OR 
  (CONVERT( [ContactTitle], 'System.String') like '%Mexico%' ) OR 
  (CONVERT( [Address], 'System.String') like '%Mexico%' ) OR 
  (CONVERT( [City], 'System.String') like '%Mexico%' ) OR 
  (CONVERT( [Region], 'System.String') like '%Mexico%' ) OR 
  (CONVERT( [PostalCode], 'System.String') like '%Mexico%' ) OR 
  (CONVERT( [Country], 'System.String') like '%Mexico%' ) OR 
  (CONVERT( [Phone], 'System.String') like '%Mexico%' ) OR 
  (CONVERT( [Fax], 'System.String') like '%Mexico%' )) AND ( 
  (CONVERT( [CustomerID], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [CompanyName], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [ContactName], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [ContactTitle], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [Address], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [City], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [Region], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [PostalCode], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [Country], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [Phone], 'System.String') like '%Owner%' ) OR 
  (CONVERT( [Fax], 'System.String') like '%Owner%' ))"

Et voila - that's it!

History

  • 07/02/2006: Initial release.

License

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

Share

About the Author

Thomas Haller
Software Developer (Senior)
Austria Austria
Thomas, living in austria, is a Softwaredeveloper focused on C# and MS Sql Server since 2004. He started software development at the age of 8 with Basic for Amiga 500, without great success, due his lack of english skills...
But the enthusiasm for software engeneering came back at the age of 15, and is still there Smile | :)
Nowadays, at the age of 30, Thomas is very fascinated about the possibilities of neural Networks, and is studying this at the university http://www.fernuni-hagen.de/ in addition to his regular work. (Blended Learning)

Comments and Discussions

 
QuestionExcelent Idea Pin
rubenben287-Oct-11 11:58
Memberrubenben287-Oct-11 11:58 
QuestionVery Nice Pin
GermanClayte17-Jun-09 10:29
MemberGermanClayte17-Jun-09 10:29 
GeneralGreat ideia [modified] Pin
Martin Radu6-Mar-08 7:51
MemberMartin Radu6-Mar-08 7:51 
GeneralUseful code Pin
T-C8-Oct-06 23:56
MemberT-C8-Oct-06 23:56 
GeneralNice one Pin
mokah3-Jul-06 6:13
Membermokah3-Jul-06 6:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.