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