Combobox Event Problems

FahvRay

Member
Joined
Aug 2, 2007
Messages
6
Programming Experience
1-3
I have a combobox which gets populated here on Form_Load.

cboPerson.DataSource = dsGetPersons.Tables(0)
cboPerson.DisplayMember = "FullName"
cboPerson.ValueMember = "PersonID"

After the user selects a name from the list, I need to fill some textboxes with the name.

If I use the cboPerson_SelectedIndex_Changed event, this event fires and errors during form loading on this event while passing the value to a parameter with

"Conversion from type 'DataRowView' to type 'Integer' is not valid."

If I use the cboPerson_SelectionChangeCommitted event instead, it loads fine, and works when the combobox is used with the mouse, but not when a name is typed in and Autocomplete occurs.

I really want Selected_Index_Changed, but this error must be common, right? Am I missing something to get it to work properly? I could probably use the Leave event, but this is definitely less than ideal.

Using Visual Studio 2005.

TIA
 
You should be setting the ValueMember and DisplayMember BEFORE the DataSource. The problem is that you are setting the DataSource before the ValueMember, so when the SelectedIndex first changes the SelectedValue is the item itself, a DataRowView, rather than the desired column value. If you set the ValueMember first, then the SelectedValue property will return the correct value.

Apart from that, if you don't want to execute the body of an event handler until after the form has loaded then don't. Use a Boolean variable to indicate whether the form has been initialised and don't set it to True until the end of the Load event handler. Test that variable in the other event handlers and if it's False then don't do anything.
 
Thanks!

I did end up using the boolean at first. However, every programming class I've ever had, the instructor discouraged the use of shared variables.

I guess I assumed that before assigning a datasource, it wouldn't recognize the assigned display and value names.

Live and learn. I appreciate your help.
 
jmcilhinney suggested use of a private class level variable, not a shared variable.
 
Right. I misspoke. An instance variable.

It's still not local to a procedure. Still discouraged by professors who don't ever have to get much done.
 
You should always strive to keep the scope of every variable as narrow as possible. If a variable is only accessed within a method then it should be declared within that method. If it needs to be accessed in more than one method though, it MUST be declared outside each, within the class definition, i.e. a member variable. OOP would not exist without member variables. If your instructor is discouraging the use of member variables then either they don't know what they are talking about or else they're merely over-simplifying the facts for the sake of beginners. Member variables should never be used where local variables will suffice, but there are innumerable situations where local variables will not suffice.
 
Back
Top