Combo Box Issue

MartinaL

Active member
Joined
Jun 13, 2006
Messages
30
Programming Experience
1-3
I am having an issue with a combo box on a vb page.

I have created a form in VS 2005 (vb) and have on it fields from a table called Cadets. One of the fields is state, which is an int and has a relationship with the state id in the states table.

The field on the page is a combo box which is populated by the states table and when it saves I want it to save the state id in the state column in that cadets table.

Now my issue is that when you load the form and add details when you get to the state combo box once you have selected the state out of the list (which is populating correctly) the form freezes and you can't tab off this field an go to any other fields, but there is no error in VS.

The combo box datasource is a bindingsource to the TblStatesBindingSource with Display member as State, Value Member as State ID and Selected Value as State ID. The DataBindings are to the states table bindging source with Selected item: TblStatesBindingSource - StateID, SelectedValue: TblStatesBindingSource - StateID and Text: TblStatesBindingSource - State
 
Your bindings are wrong. You should have two BindingSources: one for the States table and one for the Cadets table.

You set the DataSource of the ConboBox to the States BindingSource, the DisplayMember to to the Name column and the ValueMember to the ID column.

You now bind the SelectedValue property of the ComboBox to the StateID column of the Cadets BindingSource.

Now what happens is that the user selects a State by name from the ComboBox, which sets the SelectedValue of the ComboBox to the corresponding ID. This value will then be assigned to the current Cadet record's StateID field because that field is bound the the ComboBox's SelectedValue property. Conversely, if you set the current Cadet's StateID field in code, by loading a new record or navigating to a different record in the table, that will set the StateID field which will in turn set the ComboBox's bound SelectedValue property, which will in turn update the displayed State name.
 
Agreed.. there are too many bound properties and not enough binding sources in your example.. vs is probably getting confused or going into a loop when trying to update all the properties you have bound, hence the freezing.

Dont forget to set the combo box style to DropDownList - this will prevent the user editing the text, thereby causing an entry that is not in the list of states, which in itself will throw an error
 
Okay, it is pretty much working now accept when I first run the app the first record in the dataset shows the first state in the list no matter what is saved against it.

When I go forward to the next record it is right and when I go back to the first record it is right, it is just when it first loads up.

I have attached a screen shot of the binding details for the state dropdown.
 

Attachments

  • details.jpg
    details.jpg
    94.6 KB · Views: 36
You still have too many things bound

Remove the bindings from SelectedItem and also from Text, leaving JUST selectedValue to be bound to cadets.stateID
 
All you have to do is read my previous post. I said set the DataSource, DisplayMember and ValueMember and then bind the SelectedValue property. Had you followed those instructions it would be working.
 
Back
Top