Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to combine a rowfilter combination on 2 columns. One column is a checkbox - show only rows that are checked and at the same time also apply a text filter on another column (string). I can do each one individually but I can't seem to get a handle on the proper syntax to combine the 2 into 1 filter.

What I have tried:

My DataGridView has 2 different columns on which I apply rowfilter:
1) Checkbox Column (Call it "S")
2) Regular String Column (Call it "Artist")
3) DataTable (Call it "DT")
4) DataGridView (Call it "DGV")
and they are bound
VB
DGV.Datasource=DT

' Now I can apply each rowfilter individually to filter either columns that are checked:

dt.defaultview.rowfilter = "S"

' This will successfully show only rows in which the CheckColumn has been checked

dt.defaultview.rowfilter = String.format("{0} LIKE '{1}'", "Artist", strA)

' this successfully shows rows in which the Artist name contains variable strA
' I want to apply both conditions at once, but

dt.defaultview.rowfilter = "S" And String.format("{0} LIKE '{1}'", "Artist", strA)
doesn't work, I get an invalid cast exception
Conversion from string "S" to type 'Long' is not valid.
checkbox one is a boolean ("S") and the other is essentially a string

Any know the proper syntax to get this combination to work together?
Posted
Updated 25-May-17 0:51am
v2

1 solution

I think you should use:
dt.defaultview.rowfilter = "S=True AND " + String.format("{0} LIKE '{1}'", "Artist", strA)
 
Share this answer
 
Comments
Georg_S 27-May-17 13:56pm    
Thanks,
For reference, I actually use
String.Format("[{0}] LIKE '%{1}%'", "Artist", strA))
instead of what I wrote initially

The big thing was the AND which should be within the parenthesis
I found that
"S AND " + String.format{"[{0}] LIKE '%{1}%'", "Artist", strA)
without the =True, also works

I guess my problem was seeing .rowfilter as an expression to be evaluated instead of a string representation of the expression to be evaluated

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