Results 1 to 5 of 5

Thread: [RESOLVED] Getting subsequent filters to not affect original filters

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    23

    Resolved [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

    Would love some help here thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim criteria As New List(Of String)
    2.  
    3. If x Then
    4.     criteria.Add("x = 1")
    5. End If
    6.  
    7. If y Then
    8.     criteria.Add("y = 2")
    9. End If
    10.  
    11. If z Then
    12.     criteria.Add("z = 3")
    13. End If
    14.  
    15. Dim filter = String.Join(" AND ", criteria)
    That will give you the correct expression no matter what combination of criteria you use.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    23

    Re: Getting subsequent filters to not affect original filters

    Quote Originally Posted by jmcilhinney View Post
    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.



    example
    Code:
     Sheet1BindingSource.Filter = "Turbo like '" & "Y" & "'" & "Make like '" & MakeIDComboBox.SelectedItem & "'"

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    23

    Re: Getting subsequent filters to not affect original filters

    Ahhhhh AND not &

    >.>

    +rep

    thanks jm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width