Very strange ComboBox databinding problem
Hi,
this problem really drives me nuts. I have a custom ComboBox, which derives from the normal ComboBox control. I'm setting the DataSource of the ComboBox in my code to a DataTable and also set DisplayMember and ValueMember. So, the binding in general works fine, but here's the odd part:
Depending on what is selected I color the background of the ComboBox differently. To achieve this effect when the DataSource changed i overwrite the OnDataSourceChanged method:
| |
protectedoverridevoid OnDataSourceChanged(EventArgs e) { SelectionChanged(); base.OnDataSourceChanged(e); }
|
The selction changed method does all the coloring and here is what really buzzes me. When the method is triggered, I evalute this.Text and this.SelectedValue. However, both of them are empty after OnDataSourceChanged is entered. Anyways, when I set a debug point at the code that checks if this.Text is empty, this.Text is indeed "", however, when I now explore all the properties and fields of "this" in the debugger, this.Text suddenly changes to the expected value. If I continue to run the code the value and coloring shows up just fine. However, when I let it run through without setting the debug point, this.Text stays empty and it gets colored incorrectly since the text actually shows up in the control afterwards.
Any ideas?
[1682 byte] By [
TomFrey] at [2007-12-16]
Without replicating your specific problem, I'm guessing...but, when you change the DataSource for a combobox, the selection gets wiped out. You need to reset it.
Probably you are seeing the odd behaviour because SelectionChanged() is called before calling the base method. Try moving SelectionChanged() after the base.OnDataSourceChanged call.
OnDataSourceChanged() is called after the data source is set but before the ComboBox is filled with the data source items. As Durstin has pointed out, by calling the base implementation first, you will ensure the ComboBox is filled prior to your code being called. Note that the values of this.Text and this.SelectedValue will be invalid until after you step past base.OnDataSourceChanged(). Possibly the issue you are seeing is you are stepping past base.OnDataSourceChanged() causing the values to then appear.
Note that calls to this.Text and this.SelectedValue prior to base.OnDataSourceChanged() will result in exceptions that are caught and handled by ComboBox.
Joe
ahhh ... I found my error ...
I changed the DataSource after the InitializeComponent() of the hosting form. It works when I do it in the Load method of the form. Can someone tell me what the difference is?
Anyways, thanks a lot for all your help
The ComboBox Text does not get set until it's handle is created and this doesn't happen until the Form is made visible (after the Form constructor) - so any checks on the ComboBox.Text prior to Form.Load will return string.Empty.
Also, is your data source filled in InitializeComponent()? If not, then this will also cause problems as there will be no SelectedValue or Text value.
Joe
Nope, as I indicated, now I fill it in the Form Load method and everything works.. so, problem solved ;)
Thanks a lot for the explanation and your help.