|
Here is something that I posted on the IdeaBlade NewsGroup last winter on how to populate ComboBoxes when the list of entities in the ComboBox changes as the result of the currently selected item in some other ComboBox.
A Customer asked:
On my windows form I have a drop down with our country's Provinces and i have a drop down of cities, the cities have a relationship in the Database with the Provinces, I'm wondering now how can I get the cities to only show those linked to a specific province when this one is selected?
I replied:
I will show how to do this with a form that edits SalesOrders. Each SalesOrder has an asssociated Province and City. As you stop on a particular SalesOrder, the ComboBox for Cities will show you only cities for that Province. If you select a different province from the Province ComboBox, the ComboBox for Cities will immediately display all of the cities from the new province.
Here is how to do this:
(1) Using the designer for the ControlBindingManager, create a ComboBox for SalesOrder.Province. Populate the EntityList for this ComboBox with the list of all Provinces.
(2) Using the designer for the ControlBindingManager, create a ComboBox for SalesOrder.Cities. Populate the EntityList for this ComboBox, not with all of the Cities in the Citiy Table, but rather with the list of all Cities that are in the currently selected Province..
(3) Create an event handler for the BindingSource.CurrentChanged of SalesOrder. Record the identity of the current SalesOrder (i.e., lastSalesOrder)
(4) Create an event handler for the BindingSource.CurrentChanged of Province. This event handler needs to populate the EntityList for Cities when the CurrencyManager changes its position. There are two cases. The first case is when we are moving from SalesOrder to SalesOrder. This requires repopulating the EntityList with Province.Cities from the new Province in the new SalesOrder. The second case is when when we are changing the Province in the current SalesOrder. This requires repopulating the EntityList with Province.Cities from the newly selected Province in the ComboBox. In addition, we must choose a new SalesOrder.City value. We don't know which City to pick, so we just pick the first one. We decide whether we are in case 1 or case 2 by comparing the current SalesOrder to lastSalesOrder
|