Results 1 to 13 of 13

Thread: Filter DataGrid using Combobox

  1. #1
    Junior Member
    Join Date
    Nov 11
    Posts
    26

    Filter DataGrid using Combobox

    In a simple form (Form1) I have one combobox (ComboBox1) and one datagrid (DataGridView1).

    Combobox1 data source is a Access 2010(.accdb) table (tblCoustomers; CID is the ValueMember, CName is the DisplayMember)
    DataGridView1 data source is from the same database (tblInvoice)

    tblInvoice have those columns:
    InvID, InvNumber, InvDate, CID

    When I change the value in the ComboBox , i need that the DataGridView to show only the informations corresponding to combobox ValueMember.


    Code:
     Private Sub ComboBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    
            Me.BindingSource1.Filter = String.Format("CID LIKE '{0}%'", ComboBox1.ValueMember)
            DataGridView1.Visible = True
    End Sub
    I have also tried, without success:

    Me.BindingSource1.Filter = String.Format("Field1 LIKE '{0}%' OR Field2 LIKE '{0}%'",Me.ComboBox1.ValueMember)
    Me.BindingSource1.Filter = String.Format("Name LIKE '%{0}%'", Me.ComboBox1)
    Me.BindingSource1.Filter = "CID = " & ComboBox1.ValueMember
    Me.BindingSource1.Filter = "CID = '" & ComboBox1.ValueMember & "'"
    Me.BindingSource1.Filter = "CID Like '%" & ComboBox1.ValueMember & "%'"

    The combobox show the DisplayMembers correctly, the DataGrid show ALL the data, but I can't manage to filter those data.

    I use VB 2010

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,471

    Re: Filter DataGrid using Combobox

    So you didn't like the ComboBox's .SelectedItem property and decided to invent your own?

  3. #3
    Junior Member
    Join Date
    Nov 11
    Posts
    26

    Re: Filter DataGrid using Combobox

    There is not a _SelectedItem" propriety in combobox VB 2010

    I 've changed "ComboBox1_SelectedIndexChanged_1" to "ComboBox1_SelectedValueChanged", but the DataGridView1 still show all the data. I need only the filtered data (where Combobox CID = Datagridview CID)
    Last edited by Retelist; Aug 29th, 2012 at 11:56 PM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,768

    Re: Filter DataGrid using Combobox

    Quote Originally Posted by dunfiddlin View Post
    So you didn't like the ComboBox's .SelectedItem property and decided to invent your own?
    Oops! That's not the right property.

    There is a SelectedItem property but it's not what you want in this case. The ValueMember is the name of a column and that doesn't change. What you want is the value from that column that corresponds to the item selected by the user. You get that from the SelectedValue property.

  5. #5
    Junior Member
    Join Date
    Nov 11
    Posts
    26

    Re: Filter DataGrid using Combobox

    OK, but after I changed the propriety to SelectedValue, it still do not filter...

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,768

    Re: Filter DataGrid using Combobox

    What data type is the CID column? If it's not text then you can't use LIKE and you don't wrap it in single quotes.

  7. #7
    Junior Member
    Join Date
    Nov 11
    Posts
    26

    Re: Filter DataGrid using Combobox

    CID is Number (Access 2010), CName is Text

    I also tried:
    Me.BindingSource1.Filter = String.Format("CID = '", ComboBox1.ValueMember)

    without success...
    Last edited by Retelist; Aug 30th, 2012 at 02:31 AM.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,768

    Re: Filter DataGrid using Combobox

    Then there's your issue. The Filter property of a BindingSource basically represents a SQL WHERE clause so it must obey the same rules. In SQL you don't wrap numbers in single quotes and you cannot use the LIKE operator or wildcards. You can only numerical operators, e.g. = or <. This is the only example of yours that uses valid syntax:
    Code:
    Me.BindingSource1.Filter = "CID = " & ComboBox1.ValueMember
    although that would have to be changed to use the SelectedValue property.

  9. #9
    Junior Member
    Join Date
    Nov 11
    Posts
    26

    Re: Filter DataGrid using Combobox

    I have attached the project and database (into a .zip archive)... Please, take a look..

    VBTest.zip

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,768

    Re: Filter DataGrid using Combobox

    No need. Already explained what to do.

  11. #11
    Junior Member
    Join Date
    Nov 11
    Posts
    26

    Re: Filter DataGrid using Combobox

    I did that, without success....

    my code:

    First attempt:
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
            Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
            'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
            Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
            Me.DataGridView1.Visible = False
        End Sub
    
        Private Sub ComboBox1_ValueMember(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Me.BindingSource1.Filter = "CID =" & ComboBox1.ValueMember
            Me.DataGridView1.Visible = True
        End Sub
    End Class
    Nothing happened... no filtering


    Second Attempt:
    Code:
    Public Class Form1
    
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
            Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
            'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
            Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
            Me.DataGridView1.Visible = False
        End Sub
    
        Private Sub ComboBox1_ValueMemberChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Me.InvoicesBindingSource.Filter = "CID =" & ComboBox1.ValueMember
            Me.DataGridView1.Visible = True
        End Sub
    End Class
    Error: "Syntax error: Missing operand after '=' operator."

    I attached the vb project and database...

  12. #12
    Junior Member
    Join Date
    Nov 11
    Posts
    26

    Re: Filter DataGrid using Combobox

    third attempt:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
            Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
            'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
            Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
            Me.DataGridView1.Visible = False
        End Sub
    
        Private Sub ComboBox1_SelectedValue(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
            Me.InvoicesBindingSource.Filter = "CID =" & ComboBox1.ValueMember
            Me.DataGridView1.Visible = True
        End Sub
    End Class
    Error: An error occurred creating the form. See Exception.InnerException for details. The error is: Syntax error: Missing operand after '=' operator.

    fourth attempt:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
            Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
            'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
            Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
            Me.DataGridView1.Visible = False
        End Sub
    
        Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
            Me.InvoicesBindingSource.Filter = "CID =" & ComboBox1.ValueMember
            Me.DataGridView1.Visible = True
        End Sub
    End Class
    Same error as above...

  13. #13
    Junior Member
    Join Date
    Nov 11
    Posts
    26

    Re: Filter DataGrid using Combobox

    Solveeeeeed!!!!

    Working code:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'VBTestDataSet.Customer' table. You can move, or remove it, as needed.
            Me.CustomerTableAdapter.Fill(Me.VBTestDataSet.Customer)
            'TODO: This line of code loads data into the 'VBTestDataSet.Invoices' table. You can move, or remove it, as needed.
            Me.InvoicesTableAdapter.Fill(Me.VBTestDataSet.Invoices)
    
            Me.DataGridView1.Visible = False
        End Sub
    
        Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
            Me.InvoicesBindingSource.Filter = "CID ='" & ComboBox1.SelectedValue & "'"
            Me.DataGridView1.Visible = True
        End Sub
    End Class
    Thanks jmcilhinney !

Posting Permissions

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