[RESOLVED] Getting subsequent filters to not affect original filters
Hey all.
I'm designing a form wherein a datagrid is being filtered from numerous controls.
The issue im having is each filter overrides the previous one. Just wondering the best way to approach this problem.
Heres a snippet of my code:
Code:
Private Sub MakeIDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeIDComboBox.SelectedIndexChanged
Sheet1BindingSource.Filter = "Make like '" & MakeIDComboBox.SelectedItem & "'"
End Sub
Private Sub turboCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles turboCheck.CheckedChanged
If (turboCheck.Checked = True) Then
Sheet1BindingSource.Filter = "Turbo like '" & "Y" & "'"
End If
With the result: http://i53.tinypic.com/2w7hdns.jpg
(it should only show turbo aflas)
I've tried a variety of other approaches to no avail including
Code:
Private Sub turboCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles turboCheck.CheckedChanged
If (turboCheck.Checked = True) Then
Sheet1BindingSource.Filter = "Turbo like '" & "Y" & "'" & "Make like '" & MakeIDComboBox.SelectedItem & "'"
End If
(syntax errors)
and
Code:
Private Sub turboCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles turboCheck.CheckedChanged
If (turboCheck.Checked = True) Then
Sheet1BindingSource.Filter = "Turbo like '" & "Y" & "'"
Sheet1BindingSource.Filter = "Make like '" & MakeIDComboBox.SelectedItem & "'"
(one still overrides the other)
I'm really new to this so I'm sure the solution is probably rather simple :blush:
Would love some help here thanks!
Re: Getting subsequent filters to not affect original filters
The Filter expression is basically a SQL WHERE clause. Just like in a WHERE clause, if you want to use multiple criteria then you have to AND and OR them together in the appropriate manner. If you want to filter out records other than those WHERE Turbo is LIKE one value AND Make is LIKE another then that's exactly what you have to put in your Filter expression.
If the criteria might change dynamically then the best way to build the complete expression is to create a List containing the subexpressions and then Join them together with ANDs between them, e.g.
vb.net Code:
Dim criteria As New List(Of String)
If x Then
criteria.Add("x = 1")
End If
If y Then
criteria.Add("y = 2")
End If
If z Then
criteria.Add("z = 3")
End If
Dim filter = String.Join(" AND ", criteria)
That will give you the correct expression no matter what combination of criteria you use.
Re: Getting subsequent filters to not affect original filters
Quote:
Originally Posted by
jmcilhinney
Just like in a WHERE clause, if you want to use multiple criteria then you have to AND and OR them together in the appropriate manner.
Thanks for the quick response as always jm. I tried doing this like i showed in my second and third snippets of code but the syntax is wrong. I;ve tried switching them around, removing &'s, removing "'s....nothing seems to fit.
:confused:
example
Code:
Sheet1BindingSource.Filter = "Turbo like '" & "Y" & "'" & "Make like '" & MakeIDComboBox.SelectedItem & "'"
Re: Getting subsequent filters to not affect original filters
Add this line of code after yours:
Code:
Sheet1BindingSource.Filter = "Turbo like '" & "Y" & "'" & "Make like '" & MakeIDComboBox.SelectedItem & "'"
MessageBox.Show(Sheet1BindingSource.Filter)
Look at your Filter value and then go back and read my post again. Did you do what I told you to do?
Re: Getting subsequent filters to not affect original filters
Ahhhhh AND not &
>.>
+rep
thanks jm :D