Solved

Filter a combobox based on the other Combobox

Posted on 2010-09-22
17
540 Views
Last Modified: 2012-05-10
Hello,

I have a windows form created in VB.Net with two combo box listing. I want the third combobox to filter the data based on the first two combo box
For example
First combo box: Category
Second Combo Box: Manufacturer
Third Combo Box: Model

On selection of the first two combobox, the third combo box should filter data.  

Third combo box data source is from a table that holds all the three data. but display member is Model


How can it be done. please assist. Thank you
0
Comment
Question by:angel7170
  • 11
  • 3
  • 3
17 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33736568
Hi
All comboboxes are Databound?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33736828
ok try this...
Private Sub ComboBox1_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDownClosed

        Dim CustID As Integer = ComboBox1.SelectedValue

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Dim bs As New BindingSource

        bs.DataSource = Me.Combo3DataSet

        bs.DataMember = "MyCombobox3TableName"



        bs.Filter = ("Key_Model=" & CustID)



        Dim col As New ComboBox

        col = Me.ComboBox3



        col.DataSource = bs

        col.DisplayMember = "Model"

        col.ValueMember = "Key_Model"    '>>>>>For Example

    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33736893
CompleteCode...Because we have to check also the DropDown Event
Private Sub ComboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown

        Try



            Dim CustID As Integer = Me.ComboBox1.SelectedValue

           





            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            Dim bs As New BindingSource

             bs.DataSource = Me.Combo3DataSet

        bs.DataMember = "MyCombobox3TableName"



             bs.Filter = ("Key_Model=" & CustID)



            Dim col As New ComboBox

            col = Me.ComboBox3



            col.DataSource = bs

            col.DisplayMember = "Model"

        col.ValueMember = "Key_Model"    '>>>>>For Example

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''







        Catch ex As InvalidCastException



        Catch ex As Exception



            MsgBox(ex.ToString)



        End Try

    End Sub



    Private Sub ComboBox1_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDownClosed

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Dim bs As New BindingSource

        bs.DataSource = Me.Combo3DataSet

        bs.DataMember = "MyCombobox3TableName"



        bs.Filter = ("Key_Model=" & CustID)



        Dim col As New ComboBox

        col = Me.ComboBox3



        col.DataSource = bs

        col.DisplayMember = "Model"

        col.ValueMember = "Key_Model"    '>>>>>For Example

    End Sub

Open in new window

0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 33737131
0
 

Author Comment

by:angel7170
ID: 33737169
I tried this code with modifications but doesn't seem working. I have also attached the sreenshot of what I see in the model combo box...
Please assist.


Private Sub cmb_Category_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_Category.DropDown

        Try

            Dim Category As String = cmb_Category.Text

            Dim Manu As String = cmb_Manufacturer.Text

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            Dim bs As New BindingSource

            bs.DataSource = Me.TBLMODELBindingSource

            bs.DataMember = "MODEL"



            bs.Filter = ("MODEL like '" + Category + "'")



            Dim col As New ComboBox

            col = Me.cmb_Model



            col.DataSource = bs

            col.DisplayMember = "MODEL"

            'col.ValueMember = "MODEL"    '>>>>>For Example

        Catch ex As InvalidCastException



        Catch ex As Exception



            MsgBox(ex.ToString)



        End Try



    End Sub



    Private Sub cmb_Category_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_Category.DropDownClosed

        Dim Category As String = cmb_Category.Text

        Dim Manu As String = cmb_Manufacturer.Text

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Dim bs As New BindingSource

        bs.DataSource = Me.TBLMODELBindingSource

        bs.DataMember = "MODEL"



        bs.Filter = ("MODEL like '" + Category + "'")



        Dim col As New ComboBox

        col = Me.cmb_Model



        col.DataSource = bs

        col.DisplayMember = "MODEL"

        'col.ValueMember = "MODEL"    '>>>>>For Example



    End Sub

Open in new window

test.doc
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33737237
just a note:
in your form load set :
Me.Combo1DataSet.EnforceConstraints = False  'Combo1DataSet is the name of your dataset in combobox1
Me.Combo3DataSet.EnforceConstraints = False  ''Combo2DataSet is the name of your dataset in combobox2

Also
"MyCombobox3TableName"  is the table or query ,the one you create the dataset which is bounded in combobox3


try it and you'll see ..it works fine...
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33737288
try to use selected value with the value member and amke the filter as my code.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33737320
col.DisplayMember = "MODEL"
            'col.ValueMember = "MODEL"    ?? why the same?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33737429
try this

Private Sub cmb_Category_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_Category.DropDown

        Try

            Dim Category As String = cmb_Category.SelectedValue

            Dim Manu As  = cmb_Manufacturer.Text

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            Dim bs As New BindingSource

            bs.DataSource = Me.TBLMODELBindingSource

            bs.DataMember = "MODEL"



            bs.Filter = ("Model=" & Category) 



            Dim col As New ComboBox

            col = Me.cmb_Model



            col.DataSource = bs

            col.DisplayMember = "MODEL"

            'col.ValueMember = "MODEL"    '>>>>>For Example

        Catch ex As InvalidCastException



        Catch ex As Exception



            MsgBox(ex.ToString)



        End Try



    End Sub



    Private Sub cmb_Category_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmb_Category.DropDownClosed

        Dim Category As String = cmb_Category.Text

        Dim Manu As String = cmb_Manufacturer.SelectedValue

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Dim bs As New BindingSource

        bs.DataSource = Me.TBLMODELBindingSource

        bs.DataMember = "MODEL"



       bs.Filter = ("Model=" & Category) 



        Dim col As New ComboBox

        col = Me.cmb_Model



        col.DataSource = bs

        col.DisplayMember = "MODEL"

        'col.ValueMember = "MODEL"    '>>>>>For Example



    End Sub

Open in new window

0
 

Author Comment

by:angel7170
ID: 33737566
There is no option for EnforseConstraints...My combo boxes are bound to Binding source.

Me.Combo1DataSet.EnforceConstraints = False  'Combo1DataSet is the name of your dataset in combobox1
Me.Combo3DataSet.EnforceConstraints = False  ''Combo2DataSet is the name of your dataset in combobox2
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33737655
have you checked my article?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33737755
did you try the alst post?
it works perfect on me.
@emoreau Could you please post a link?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33737812
@angel7170
Can you post both DisplayMember and ValueMember from ComboBox1 and 3?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 33737900
>>@emoreau Could you please post a link?
see post 33737131
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33737938
OOOOhhhh ... sorry ...i didn't Scroll up the page...
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33738056
Slightly Close...@emoreau my post Works (Slightly Close to yours)...But Your Article is Perfect..The only thing i have to do is to quit the Question...
@angel7170 read this article..there is more analysis..and you'll understand better what is going on ...!!thanks @emoreau i will keep the code too...!!
0
 

Author Comment

by:angel7170
ID: 33738310
ok, thank you ..both. I didn't get a chance to read through the article...but will do so.. thanks again.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now