below is a sample code as to how you can achieve your goal.
To use the code create a new windowsApplication, then drag two combobox(name it cmbState and cmbCity) to the form(form1) then paste the code to form1
(Note: don't get confuse by the amount of code.
The actual implementation is in cmbState_SelectionChangeCommitted() and form1_load() events.
The generateData() simply generates data to be fed to the combobox)
Dim stateDT As DataTable 'in memory table for storing state data (datatable can easily be filled with data from your Database)
Dim cityDt As DataTable ' in memory table for storing city data (datatable can easily be filled with data from your Database)
'bindingSource has a filter property which we will make use of to filter the cityDT as per the state chosen
Dim cityBS As BindingSource
''' <summary>
''' initialize and fill stateDT and cityDt with state and city data respectively
''' note: to be able to chose city from the state chosen we need a connection between these two data groups and we
''' have 'stateSno' in both citydt and statedt as the connecting column
''' </summary>
''' <remarks></remarks>
Sub generateData()
stateDT = New DataTable
stateDT.Columns.Add("stateSno", GetType(Integer))
stateDT.Columns.Add("stateName", GetType(String))
stateDT.Rows.Add(0, "Newyork")
stateDT.Rows.Add(1, "California")
cityDt = New DataTable
cityDt.Columns.Add("stateSno", GetType(Integer)) 'this is a related column to stateDt->stateSNo ,so we just need to use filter
cityDt.Columns.Add("cityName", GetType(String))
'cities for Newyork
cityDt.Rows.Add(0, "NY_city1")
cityDt.Rows.Add(0, "NY_city2")
'cities for California
cityDt.Rows.Add(1, "Cal_city1")
cityDt.Rows.Add(1, "Cal_city2")
cityDt.Rows.Add(1, "Cal_city3")
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
generateData()
'here we feed state data to the State combobox using datasource property
cmbState.DataSource = stateDT 'feed state data directly to combobox
cmbState.DisplayMember = "stateName" 'the member displayed in the combobox text
cmbState.ValueMember = "stateSno" 'the value returned when we call combobox.selectedvalue
cityBS = New BindingSource 'I suggest you get use to using binding source if you need to filter or sort data
cityBS.DataSource = cityDt 'feed city data to cityBS
cmbCity.DataSource = cityBS ' then feed cityBS to the city combobox so that we can use cityBS's filter property
cmbCity.DisplayMember = "cityName" 'the member displayed in the combobox text
'explicitly set state to index 0 and call cmbState_SelectionChangeCommitted() to filter city as the per the state selected
cmbState.SelectedIndex = 0
cmbState_SelectionChangeCommitted(sender, e)
End Sub
Private Sub cmbState_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles cmbState.SelectionChangeCommitted
If cmbState.SelectedIndex < 0 Then
cityBS.Filter = "stateSNo = -1" 'if no item is selected then we set it to a state no.(in this case -1) which we know will never be assigned to a state
Else
cityBS.Filter = "stateSNo = " & cmbState.SelectedValue 'simply filter cityBS with the cmbState selectedValue to get the desired result
End If
End Sub