Click here to Skip to main content
Licence CPOL
First Posted 2 Jul 2006
Views 21,566
Downloads 336
Bookmarked 26 times

Support filtering and searching on multiple columns with RowFilterBuilder

By | 2 Jul 2006 | Article
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)

About the Author

Thomas Haller

Web Developer

Austria Austria

Member

Thomas (23) , living in austria, is a Softwaredevelopment beginner, who hopes that he is on the best way to become a professional. He started software development at the age of 16 with Delphi. He is very often unsure about what is the best for him - so he changed technology very often - starting with Delphi - switched to Networking - tryed Web Development with HTML-CSS-PHP-MySql - back to Windows Programming with VB - uh Databases are Fun Smile | :) - C/C++ - MFC - COM.... Now, thinks the .NET Enviroment is the best for him, so he passed the MCP Exams for C# Windows Forms and Designing SQL Server 2000 Databases this year - moving forward to become a MCAD.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionExcelent Idea Pinmemberrubenben2811:58 7 Oct '11  
QuestionVery Nice PinmemberGermanClayte10:29 17 Jun '09  
GeneralGreat ideia [modified] PinmemberMartin Radu7:51 6 Mar '08  
GeneralUseful code PinmemberT-C23:56 8 Oct '06  
GeneralNice one Pinmembermokah6:13 3 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 2 Jul 2006
Article Copyright 2006 by Thomas Haller
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid