angel7170
asked on
Filter a combobox based on the other Combobox
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
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
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
test.doc
just a note:
in your form load set :
Me.Combo1DataSet.EnforceCo nstraints = False 'Combo1DataSet is the name of your dataset in combobox1
Me.Combo3DataSet.EnforceCo nstraints = 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...
in your form load set :
Me.Combo1DataSet.EnforceCo
Me.Combo3DataSet.EnforceCo
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...
try to use selected value with the value member and amke the filter as my code.
col.DisplayMember = "MODEL"
'col.ValueMember = "MODEL" ?? why the same?
'col.ValueMember = "MODEL" ?? why the same?
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
ASKER
There is no option for EnforseConstraints...My combo boxes are bound to Binding source.
Me.Combo1DataSet.EnforceCo nstraints = False 'Combo1DataSet is the name of your dataset in combobox1
Me.Combo3DataSet.EnforceCo nstraints = False ''Combo2DataSet is the name of your dataset in combobox2
Me.Combo1DataSet.EnforceCo
Me.Combo3DataSet.EnforceCo
have you checked my article?
did you try the alst post?
it works perfect on me.
@emoreau Could you please post a link?
it works perfect on me.
@emoreau Could you please post a link?
@angel7170
Can you post both DisplayMember and ValueMember from ComboBox1 and 3?
Can you post both DisplayMember and ValueMember from ComboBox1 and 3?
>>@emoreau Could you please post a link?
see post 33737131
see post 33737131
OOOOhhhh ... sorry ...i didn't Scroll up the page...
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...!!
@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...!!
ASKER
ok, thank you ..both. I didn't get a chance to read through the article...but will do so.. thanks again.
All comboboxes are Databound?