Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > ComboBox DataBinding
 

ComboBox DataBinding

I have Customers table like CustomerID, CountryID, CityID, VillageID. and also Country, City and Village table.

I want to enter data to Customers table. When entering data, user choose Country, City and Village by ComboBox and these comboboxes are filled by releated tables Country, City and Village.

Moreover, when user choose Country, there should be only releated cities at City ComboBox. Also village will works same.

If I fill customer table before filling country, city and village table, combobox selected values are wrong. I mean Country combobox dont show correct CountryName according to Customers.CountryID. For these reason I must fill Country, City and Village tables before filling Customers tables. But now, all cities are shown, I need to show only selected country's cities.

My problem is how can I use releated combobox data binding?
MigrationUser 1  Thursday, May 05, 2005 3:02 AM

Are you using DataSets?  If so, you need setup your binding to use the foreign key between the tables.  You can do this using BindingSources.  Assume you have the following tables that are all in the CustomersDataSet (and related in that data set).

Customers has CountryID, CityID, VillageID
Countries
Cities has CountryID
Villages as CityID

You'll have 4 BindingSources:

CustomersBindingSource
    DataSource = CustomersDataSet
    DataMember="Customers"
CountriesBindingSource
   DataSource = CustomersDataSet
   DataMember="Countries"
CitiesBindingSource
   DataSource = CountriesBindingSource
   DataMember = "FK_Countries_Cities" (the foreign key name between countries and orders)
VillagesBindingSource
   DataSource = CitiesBindingSource
   DataMember = ""FK_Cities_Villages" (the FK between cities and villages)

ComboBox binding:

CountriesComboBox.DataSource = CountriesBindingSource
CountriesComboBox.DataBindings.Add("SelectedValue, CustomersBindingSource, "CountryID", true);

CitiesComboBox.DataSource = CitiesBindingSource
CitiesComboBox.DataBindings.Add("SelectedValue, CustomersBindingSource, "CityID", true);

CitiesComboBox.DataSource = VillagesBindingSource
CitiesComboBox.DataBindings.Add("SelectedValue, CustomersBindingSource, "VillageID", true);

Joe
MigrationUser 1  Friday, May 06, 2005 11:07 AM

You can use google to search for other answers

Custom Search

More Threads

• Bound or Unbound
• CellValidating usage
• Showing large text in datagridview cell
• Strongly Typed Dataset Problem
• Datagridview - Data Manipulation Questions in vb.net
• A list<string> as a datasource to a combobox in a datagrid ????????
• Checkbox as selected in window applictaion datagridview control
• DataGridView - How to log changes to a cell?
• DATAGRID
• Disabled DataGridViewComboBoxCell