Results 1 to 13 of 13

Thread: [RESOLVED] DGV Combined Filter

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Resolved [RESOLVED] DGV Combined Filter

    Hi,

    I am filtering a Datagridview with combined help of 4 textboxes and 4 comboboxes each dedicated to a field in DB. All i know about this process is i can combine two or more filters but how do i check at a given time, whether which of these (cb & tb) have been used already to perform the filter already and then collect all the instances and perform the collective filter.

    Any suggestions??

    Lux.

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

    Re: DGV Combined Filter

    What values have been used to filter before is irrelevant. Just build the entire filter string each time.
    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
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: DGV Combined Filter

    Now suppose i have these three filters:

    Code:
    ClientProfileBindingSource.Filter = String.Format("Convert(MachineID, 'System.String') LIKE '{0}%'", TextBox2.Text)
    and

    Code:
    ClientProfileBindingSource.Filter = "Category = '" & ComboBox2.Text & "'"
    and

    Code:
    ClientProfileBindingSource.Filter = String.Format("ClientType = '{0}' OR ClientType = '{1}'", "Seeker", "Both")
    You mean to say that under all of their event change, i should use a single combined filter?

    Code:
    ClientProfileBindingSource.Filter = String.Format("Convert(MachineID, 'System.String') LIKE '{0}%'", TextBox2.Text) AND "Category = '" & ComboBox2.Text & "'" AND String.Format("ClientType = '{0}' OR ClientType = '{1}'", "Seeker", "Both")
    When i am using the above code i am getting error:
    Conversion from string "Convert(MachineID, 'System.Strin" to type 'Long' is not valid.
    How can it be resolved?

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: DGV Combined Filter

    Check this one

    Code:
    ClientProfileBindingSource.Filter 
    =String.Format("(Convert(MachineID, 'System.String') 
    LIKE '{0}%') AND (Category ='{1}' ) AND 
     (ClientType = '{2}' OR ClientType = '{3}' ) ",
    
                                                      TextBox2.Text, ComboBox2.Text,"Seeker", "Both")
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: DGV Combined Filter

    i used this:

    vb Code:
    1. ClientProfileBindingSource.Filter = String.Format("(Convert(MachineID,'System.String') LIKE '{0}%') AND (Convert(FirstName,'System.String') LIKE '{1}%') AND (Category = '{2}') AND (Convert(LastName,'System.String') LIKE '{3}%') AND (Convert(Mobile,'System.String') LIKE '{4}%') AND (Type = '{5}') AND (Brand = '{6}') AND (Model = '{7}') AND (State = '{8}') AND (District = '{9}') AND (City = '{10}') AND (Convert(ClientID,'System.String') LIKE '{11}%') AND (Availability = '{12}')", TextBox2.Text, Cname.Text, ComboBox2.Text, TextBox3.Text, TextBox4.Text, ComboBox4.Text, ComboBox3.Text, ComboBox5.Text, ComboBox1.Text, ComboBox6.Text, ComboBox7.Text, ComboBox8.Text)

    and i got the following error:

    Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: DGV Combined Filter

    Check the format arguments it is 12

    TextBox2.Text, Cname.Text, ComboBox2.Text, TextBox3.Text, TextBox4.Text, ComboBox4.Text, ComboBox3.Text, ComboBox5.Text, ComboBox1.Text, ComboBox6.Text, ComboBox7.Text, ComboBox8.Text
    and you are passing only 11
    Please mark you thread resolved using the Thread Tools as shown

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: DGV Combined Filter

    oops.. got it right.

    i have placed this code in all the event change method of the controls. But when i enter text in text box or select an item in combobox, i get no result in DGV.

    I need to perform this filter mechanism by use of any possible combinations of the controls for which i hope 'AND' is the right operator. Am i wrong?

  8. #8
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: DGV Combined Filter

    Quote Originally Posted by LuxCoder View Post
    oops.. got it right.

    i have placed this code in all the event change method of the controls. But when i enter text in text box or select an item in combobox, i get no result in DGV.

    I need to perform this filter mechanism by use of any possible combinations of the controls for which i hope 'AND' is the right operator. Am i wrong?
    For textBoxes call the the method TextBox.LeaveEvent of the textBox.

    And for the Combo Box you have call in the the Combobox _SelectedIndex Changed method.

    And for the And only you have decide whether to use AND or OR
    Please mark you thread resolved using the Thread Tools as shown

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: DGV Combined Filter

    Tried what you suggested.
    But how does it matter what even it is being used?

    Is it not that, that the other blank textboxes and comboboxes are included in the filter when they have no values in them ???

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

    Re: DGV Combined Filter

    You've got two choices:

    1. If you don't want empty fields added to the filter then don't add them. That's what If statements are for:
    vb.net Code:
    1. Dim filters As New List(Of String)
    2.  
    3. If Me.TextBox1.Text.Trim().Length > 0 Then
    4.     filters.Add(String.Format("Column1 LIKE '%{0}%'", Me.TextBox1.Text.Trim()))
    5. End If
    6.  
    7. If Me.TextBox2.Text.Trim().Length > 0 Then
    8.     filters.Add(String.Format("Column2 LIKE '%{0}%'", Me.TextBox2.Text.Trim()))
    9. End If
    10.  
    11. If Me.TextBox3.Text.Trim().Length > 0 Then
    12.     filters.Add(String.Format("Column3 LIKE '%{0}%'", Me.TextBox3.Text.Trim()))
    13. End If
    14.  
    15. Dim filter As String = String.Join(" AND ", filters)
    2. Include everything but add extra filters to account for empty fields:
    vb.net Code:
    1. Dim filter As String = String.Format("('{0}' = '' OR Column1 = '{0}') AND ('{1}' = '' OR Column2 = '{1}') AND ('{2}' = '' OR Column3 = '{2}')", _
    2.                                      Me.TextBox1.Text.Trim(), _
    3.                                      Me.TextBox2.Text.Trim(), _
    4.                                      Me.TextBox3.Text.Trim())
    Note that I use '=' rather than 'LIKE' in that second example because you don't need the extra filters for LIKE. If the field is empty then every record will match "LIKE '%%'" anyway.
    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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: DGV Combined Filter

    Thank you.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Posts
    362

    Re: [RESOLVED] DGV Combined Filter

    I am using the following to join the filter string and perform a filter on the DGV. I tried a lot but it gives me error

    Error 1 Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to '1-dimensional array of String'.
    vb Code:
    1. Dim filters As New List(Of String)
    2.                                 If (reader("Category".ToString)).Length > 0 Then
    3.                                     filters.Add(String.Format("(Convert(Category, 'System.String') LIKE '%{0}%')", (reader("Category"))))
    4.                                 End If
    5.                                 If (reader("Type".ToString)).Length > 0 Then
    6.                                     filters.Add(String.Format("(Convert(Type, 'System.String') LIKE '%{0}%')", (reader("Type"))))
    7.                                 End If
    8.                                 Dim filter As String = String.Join(" And ", filters)
    9.                                 ClientProfileBindingSource.Filter = filter

    what should i do?

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

    Re: [RESOLVED] DGV Combined Filter

    I typed that code straight into the forum so didn't have the IDE to check it for me. That said, the fix is fairly simple. You've got a List(Of String) and you need a String(). How do you create an array from a List? You call ToArray on it.
    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

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