Click here to Skip to main content
13,898,253 members
Click here to Skip to main content
Add your own
alternative version

Stats

41.5K views
1.2K downloads
27 bookmarked
Posted 2 Jul 2006
Licenced CPOL

Support filtering and searching on multiple columns with RowFilterBuilder

, 2 Jul 2006
Rate this:
Please Sign up or sign in to vote.
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.

//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)

You may also be interested in...

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 2 Jul 2006
Article Copyright 2006 by Thomas Haller
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid