Reputation: 4718
I have a little problem when setting the DataSource on a combo.
I have a helper method which I pass in a combo and a default item.
Within this method the combo's DataSource is set which fires the SelectionChanged event. The Selectionchanged event then calls this helper method again so that the DataSource for another combo can be set based on the selected ID from the first combo. This process is then repeated for 6 more combos and each time a call to the database is made to get the new data.
After the DataSource has been set within this method I then set the SelectedValue of the combo with the passed in defaultItem. This then causes the chain to start again and of course all the database calls.
What's the best way of getting round this?
I've tried using the SelectionChangeCommitted event but this doesn't start the chain off.
Thanks in advance.
I'm using C#.Net 4.0
Upvotes: 1
Views: 4182
Reputation: 9712
The examples I've seen from Microsoft handle this by hooking the selection changed event only AFTER you are done loading your data. So instead of using the designer to hook up the event you handle it on your own.
Upvotes: 0
Reputation: 617
I've used the following code to differentiate combobox SelectionChanged events causes by loading them with data (DataSource=...) vs. actual selection changes.
private void comboBoxSomeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Ignore changes that are made during initialization
if (e.AddedItems.Count != e.RemovedItems.Count)
return;
...
Please note that I am not 100% sure that this will work in your particular case but it's pretty easy to check.
Upvotes: 1
Reputation: 1128
Try unhooking the event handler before yo set the SelectedValue of the combo with the passed in defaultItem and then hook it again after that. It's no pretty but...
Edit:
I get from your comment below that because you use the same helper method for the six combos you can't hook/unhook the event because you can't tell which one of your combos you are dealing with each time. That's right, I din't notice that at first.
I have reread your question and noticed something I passed up at first, "using SelectionChangeCommited doesn't start the chain off". Well, that's right, SelecttionChangeCommited only fires when the user changes the combo selection.
To recap, you need the change to start off from within the helper method, when the combo dataSource is changed (but not when the default value is set) and also when te user changes one of the combo's selected value (the chain would start off from that combo on). I think you may keep your helper method and use a combination of OnDataSourceChanged (for triggering the chain from whithin the helper method when the dataSource is changed) and also OnSelectionChangeComitted (for triggering the chain when the user chages the selection). You don't even need to write two handlres for each combo, because both this events have the same signature so you may hook them to the same method. That is, the method hooked right now to SelectionChanged for each combo may be hooked instead to both SelectionChangeCommited and DataSourceChanged. Hope this helps.
Upvotes: 0
Reputation: 1156
It's not the prettiest, but I've usually dealt with this by setting some kind of manualSelectionChangeInProgress member field in the class, so I'd set it to true before setting SelectedValue and false after, then have a check for the field value before initiating the whole datasource process you're trying to avoid.
Upvotes: 0