ComboBox - Order in which Properties are set

ComboBox - Order in which Properties are set

Old forum URL: forums.lhotka.net/forums/t/39.aspx


Jav posted on Tuesday, May 09, 2006

I am noticing a weird problem.  Like everybody, I have many lookup comboboxes that are populated from DB using NameValue lists. Once the list is read from DB, the following properties of the combobox are set:

DataSource
DisplayMember
ValueMember

If you set these properties in the order I have given above, you may, under some circumstances, get a runtime error.  The runtime error can be obviated if you set the DisplayMember and ValueMember before the DataSource.

Here is the circumstance that causes the runtime error. It appears that the ComboBox's SelectedIndexChanged event is raised as soon as the DataSource is set.  If in your SelectedIndexChanged event handler you try to do something which depends upon the item being of a certain Type (or a multitude of other things) you may get an error because the ValueMember and DisplayMember has not been set yet and the selected item has an invalid value (even though the SelectedIndex > -1) - at least that is what I am finding.

Jav

Copyright (c) Marimer LLC