How to create a chained combobox
-
i want to create 2 combobox st combobox = country nd combobox = district/region st combobox is the country combobox. when i choose one country , the nd combobox will change / auto populate all the region based on the selected country . all the data is populate from access database . im new to VB . appreciate ur help :thumbsup:
-
i want to create 2 combobox st combobox = country nd combobox = district/region st combobox is the country combobox. when i choose one country , the nd combobox will change / auto populate all the region based on the selected country . all the data is populate from access database . im new to VB . appreciate ur help :thumbsup:
A combobox has an event for the change of a selection, e.g.
SelectionChanged
orSelectedIndexChanged
. In the corresponding event handler, get the SelectedItem of the combobox. Caution: it might be "Nothing". If it is not Nothing, query your database to get the regions. Make the regions combobox empty (e.g.comboregions.Items.Clear()
), and fill the new data into it. You could then set the SelectedIndex to 0 to preselect the first item in the combobox. -
i want to create 2 combobox st combobox = country nd combobox = district/region st combobox is the country combobox. when i choose one country , the nd combobox will change / auto populate all the region based on the selected country . all the data is populate from access database . im new to VB . appreciate ur help :thumbsup:
The solution given by Bernhard Hiller works fine. Another option is in the
DataSet
designer or programmatically, create a relation betweenCountry
andRegion
DataTables like Relation Name: CountryRegion Parent Table: Country, primary key: CountryID Child Table: Region, Foreign key: CountryID'Now create a BindingSource for Country ComboBox
Dim CountryBindingSource As New BindingSource(DataSet1, "Country")
'Then create a binding source with CountryBindingSource as the parent using the relation
Dim RegionBindingSource As New BindingSource(CountryBindingSource, "CountryRegion")
'Now set the DataSource, DisplayMember and ValueMember properties of both the ComboBoxes
comboBox1.DataSource = CountryBindingSource
comboBox1.DisplayMember = "Country"
comboBox1.ValueMember = "CountryID"
comboBox2.DataSource = RegionBindingSource
comboBox2.DisplayMember = "Region"
comboBox2.ValueMember = "RegionID"