Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Cascaded combo boxes
 

Cascaded combo boxes


I have two combobox's on a form using databinding to populate their lists. The second combo is linked to the first via a relation, so when the selection changes in combo1, the list is re-populated in combo2.

Also the SelectedValue property on both combo's is bound to a field on another data table.

The problem is that when the combo2 list is re-populated it loses its currently selected value and gets reset to the first item in the list. I would prefer it to behave as thus... if the current value matches one in the new list then select it, if there is no match then set it 'empty' (or the first item in the list, not too concerned here).

Is it possible to get things to behave like this automatically? if not, is there an event I can subscribe to thats fired when the second combo has finished re-populating its list (I will then manually reselect current value)?


Code looks like this:

BindingSource combo1Binding = new BindingSource();
combo1Binding.DataSource = dataSet1.MyTable1;

combo1.DataSource = combo1Binding;
combo1.DisplayMember = "DisplayColumnName";
combo1.ValueMember = "ValueColumnName";

BindingSource combo2Binding = new BindingSource();
combo2Binding.DataSource = combo1Binding;
combo2Binding.DataMember = "FK_MyTable2_MyTable1"; // relation name

combo2.DataSource = combo2Binding;
combo2.DisplayMember = "DisplayColumnName";
combo2.ValueMember = "ValueColumnName";


paulbau  Thursday, April 10, 2008 7:08 AM

Hi,

I am not sure if I have understood you completely. When you select a different item in the combo1, the list in combo2 will be re-populated with a new list of related items. Can you help me to understand what you mean by "if the current value matches one in the new list then select it, if there is no match then set it 'empty'"?

The current value in combo2 is related with the previous selected item in combo1, while new list of combo2is related with the new selected item in the combo1. Do you want to lookup the original value in the new populated list? If so, there is no build-in databinding support. The customization logic I can think of is using combo1.SelectedIndexChangeor SelectionChangeCommittedevent. In the event, you know the selection in the combo1 is changed, so we can find the original value in the new list combo2.Items. If the same value is found, just select it, otherwise, we set the index to -1 to make the selection empty.

Hope it helps.

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Jeffrey Tan - MSFT  Tuesday, April 15, 2008 9:03 AM

Hi,

I am not sure if I have understood you completely. When you select a different item in the combo1, the list in combo2 will be re-populated with a new list of related items. Can you help me to understand what you mean by "if the current value matches one in the new list then select it, if there is no match then set it 'empty'"?

The current value in combo2 is related with the previous selected item in combo1, while new list of combo2is related with the new selected item in the combo1. Do you want to lookup the original value in the new populated list? If so, there is no build-in databinding support. The customization logic I can think of is using combo1.SelectedIndexChangeor SelectionChangeCommittedevent. In the event, you know the selection in the combo1 is changed, so we can find the original value in the new list combo2.Items. If the same value is found, just select it, otherwise, we set the index to -1 to make the selection empty.

Hope it helps.

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Jeffrey Tan - MSFT  Tuesday, April 15, 2008 9:03 AM

You can use google to search for other answers

Custom Search

More Threads

• Combo box- How can I display two field from the table? (VB.net or VB visual studio 2005)
• DataGridView - dragging multiple items with left mouse button
• re: Print only visible columns
• adding controls to child and grand-child grids
• Concurrency Question
• Datagridview::copy row including hidden column to another datagridview
• BindingSource, Dataset and BindingNavigator, problem: DataMember property 'RECORD' cannot be found on the DataSource.
• DataBinding to a custom user control
• changing values that are in a record ??
• Need Help: Image In A Datagrid Column (code partially from here)?