Link to home
Start Free TrialLog in
Avatar of BobBarker_99
BobBarker_99

asked on

VB.net Events and Databinding

I've posted my code below.  I have 2 combo boxes (cbo1 and cbo2)

When the user selects a value from cbo1, and changes focus, I want to set the value of cbo2 based on the selected value of cbo1.  

The fetchList call works fine, it returns the selected list.

Here is the problem.  When I select an item from cbo1, and then cause it to lose focus, the below function is called, BUT cbo2 is blank.  If i select the same item again (from cbo1) and lose focus again, cbo2 is then filled properly.

Can anyone point out what I'm doing wrong?


Private Sub cbo1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbo1.LostFocus
        If cbo1.SelectedValue Is Nothing Then Exit Sub
            cbo2.DisplayMember = "DisplayString"
            cbo2.ValueMember = "Value"
            cbo2.DataSource = myClass.fetchList(cbo1.SelectedValue)
  
    End Sub

Open in new window

Avatar of guru_sami
guru_sami
Flag of United States of America image

set breakpoint at line: If cbo1.SelectedValue Is Nothing Then Exit Sub
and see the value of cbo1.SelectedValue  when the first time it is not being populated...
Could be that value is being not set for some reason ...
I think using the SelectedIndexChanged event may help.
Avatar of BobBarker_99
BobBarker_99

ASKER

I have tried setting a breakpoint.  The selected value is as expected, but the second combo box does not populate.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Found it.  It was returing results, but different results the first and second time through.  Tracked it down, the cbo1.selectedValue wasnt what I expected teh first time through.