About ComboBox and Data binding (.NET, C#)

So, it seems like I am not able to post about anything else than IT! 🙂

Anyway, I had this strange problem with the ComboBox control of the .NET 2.0 framework (but I guess the problem would have been manifest with version 1.0/1.1 too):
I had some ComboBoxes that I had been binding to some DataTables generated as the result of some Stored Procedure calls on a DB server. Everything seemed to be working until I tried to set the ComboBox.SelectedValue property manually: disaster happened and nothing worked as it should have.

The reason for this strange behaviour was that I did not add the ComboBoxes to the form Control list BEFORE binding the source!
The worst thing of it all is that I have not been able to find any clue about the order in which these two operations whould be done on the MSDN, nor on various developer forums!

So rememeber:

  • First add the controls to your form
  • And only then you can correctly set the DataSource

Leave a Reply

Your email address will not be published. Required fields are marked *