Combox SelectedValue databinding

G

Gary Shell

I have a pair of combo boxes on a form. Both have their SelectedValue
property bound to a column on a table called "Input_Output". One column is
called "Class" and the second is called "SubClass". Each combobox has its
datasource, displaymember and SelectedValue member bound to separate tables
thru individual datatsets thru individual data adapters. The two tables are
"Class" and "SubClass".

I use parameterized query to populate the list portion of the SubClass Combo
box based on the selection displayed in the Class combobox. When editing
existing records everything works just fine. I can select a new Class and
the SubClass combobox list content changes as expected. The text displayed
in the SubClass combo changes to the first item in the list, EVEN IF NO
SELECTION IS MADE IN THE SUBCLASS COMBO. Updates to the data adapter
successfully update the database. So far so good.

If I attempt to ADD a new record, I have trouble. Initially the Class combo
and SubClass comboboxes are blank. Selecting an item in the Class combo
cause the SubClass combobox to be repopulated, with the first entry
displayed. When I execute the following code the SubClass combobox
immediately goes blank:

Me.BindingContext(Me.DsInput_Output, "Input_Output").EndCurrentEdit()
If DsInput_Output.HasChanges(DataRowState.Added) Then
'only if there really are adds to be processed
sqldaInput_Output.Update(Me.DsInput_Output)
strTheItem = Me.cmbIO_Name.Text
End If

The SubClass combo actually goes blank on the EndCurrentEdit method. So, of
course, the database gets a null for that column when the Update method is
executed. If I set a breakpoint on the EndCurrentEdit I can see that the
SubClass combo box TEXT, and SELECTEDVALUE properties both have the expected
string value. After the EndCurrentEdit they are NOTHING.

Now the strange part. If I actually select an item in the SubCLass combo the
EndCurrentEdit and subsequent code work just fine. Why is this explicit
selection necessary? Why is the EndCurrentEdit setting the SelectedValue
property to nothing? I'm confused and frustrated.

Google searches have turned up little, except to note that apparently the
combobox is a bit buggy. Specifically when placed on a tab page. (And you
guessed it, I am using these on a tab page. Sigh.)

Anyone else fought this battle? Anyone win? Should I just use a third
party combobox and cut my losses?

Thanks,
Gary
 
G

Gary Shell

I found the root cause and a solution. There appears to be a difference in
the way the databinding works on an edit versus an add new. The dataset's
item collection is updated when I repopulate the SubClass combobox during an
Edit, but it is NOT updated when I repopulate the SubClass combobox during
an Add new. (It is updated just fine if I actually select something from the
SubClass combo in either case.)

Just before the EndCurrentEdit is executed, I can look at the SubClass
combobox SelectedValue property and the dataset item collection and see that
when editing an existing record, the two items have the same value. When
adding a new record, the SelectedValue property is correct and the dataset
item is NULL. It appears that the EndCurrentEdit, repopulates the bound
SubClass combobox, during an edit with the correct data, and during an add
new with a null. The update method that follows takes whatever is in the
dataset's item collection and sends it to the database.

My workaround was actually quite simple. Prior to executing the
EndCurrentEdit I set the dataset item collection entry to the value
contained in the SubClass combobox's SelectedValue property. Now the
EndCurrentEdit no longer sets the SelectedValue property to nothing AND the
update method sends the correct data to the database.

Whew!!!!! Two 14 hour days latter and I'm on to the next bug. <grin>

Gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top