Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a combobox and a datagridview which i want to filter. In the combobox i have [Luft]and [Bus] But in the datagridview the column i want to filter looks like this:
[ColumnOne]
 AB
 AD
 AZ
 YA
 YD
 YZ

My thought is to group them like this
[AB,AD,AZ] = Luft and [YA,YD,YZ] = Bus
That means if i select Luft in the combobox, it should look through the datagridview and filter it like this:
Combobox           ColumnOne
[Luft]                AB
                      AD
                      AZ


That's my rowFilter code so far:
C#
if (combobox1.SelectedItem != null)
   {
     try
       {
         //Check an see what's in the dgv
          DataView dv = new DataView(dt);
          dv.RowFilter = "[ColumnOne]  LIKE 'AB,AD,AZ" + combobox1.Text.Trim() + "%'";
                   datagridview1.DataSource = dv;
        }
    }


BUT it isn't working :(
Posted

1 solution

LIKE is a "wildcard" search, similar to a file list "*.txt" finding all files which have a text extension. So when you use
C#
dv.RowFilter = "[ColumnOne]  LIKE 'AB,AD,AZ" + combobox1.Text.Trim() + "%'";
It looks for a row that has a column that starts with "AB,AD,AZ" and your combobox content - not match any one of them.
SQL doesn't support regex-style comparisons, so if you want to separate strings starting with "A" from those starting with "Z" you will have to be explicit about it:
SQL
string selector = combobox1.Text.Trim();
dv.RowFilter = "(" + selector + " = 'Luft' AND LEFT([ColumnOne],1) = 'A' ) OR (" + selector + " = 'Bus' AND LEFT([ColumnOne],1) = 'Z' )";
 
Share this answer
 
Comments
mikybrain1 7-Apr-15 7:04am    
Hi Original,
I'm getting this error:
The expression contains the undefined function call left ()
??
OriginalGriff 7-Apr-15 7:18am    
What's the datasource from your DataView?
mikybrain1 7-Apr-15 7:19am    
sql server
OriginalGriff 7-Apr-15 7:33am    
:doh: You don't use SQL in RowFilter expressions, Griff!
Sorry about that...:blush:

So no LEFT function - use SUBSTRING([ColumnOne], 1, 1) instead.
mikybrain1 7-Apr-15 7:41am    
It now states
The column Luft was not found.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900