3
votes

I have a WinForms app built with VB.Net in VS 2010, and I'm scratching my head over the following issue.

I have a form with a combobox which gets bound to a datasource when the form is loaded:

 With Me.cboCompany
    .DataBindings.Clear()
    .DataSource = Me.m_dsBidResults.Tables("Company")
    .ValueMember = "company_id"
    .DisplayMember = "company_name"
    .DataBindings.Add("SelectedValue", Me.m_dsBidResults, Company.company_id")
 End With

I'm using the cboCompany.SelectionChangeCommitted event to filter a datagridview by the selected company ID:

Private Sub cboCompany_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCompany.SelectionChangeCommitted
    Dim intCompanyIDN As Integer        
    intCompanyIDN = CInt(cboCompany.SelectedValue)
    SelectBidder(intCompanyIDN)  ' sub to filter datagridview, update labels      
End Sub

This seems to work fine, as long as the user doesn't switch focus to some other control and then go back to the combobox. After switching focus, if the user then changes the combobox selection to the first item in the dropdown list (SelectedIndex = 0), the SelectionChangeCommitted event fires, but the SelectedValue remains set to the previously selected value. I've verified this by adding a message box in the above event handler, displaying SelectedIndex and SelectedValue side-by-side.

'add this to SelectionChangeCommitted event handler
MsgBox(String.Format("Selected Index: {0}, Selected Value: {1}", cboCompany.SelectedIndex, cboCompany.SelectedValue))

This does NOT happen if the user changes SelectedIndex to anything other than 0; everything behaves as expected. I've verified that the table I'm binding to contains unique values for company_id and company_name.

Do I need to use some other event to verify that the SelectedValue has actually changed? Alternatively, ideas for a reliable workaround would be welcome.

1
Have you tried the other events, can you give SelectedValueChanged and SelectedIndexChanged a go instead of the SelectionChangeCommitted event that fires when an item is chosen from the drop-downlist and the drop-downlist is closed?Jeremy Thompson
Jeremy - yes, I've tried SelectedIndexChanged and SelectedValueChanged with the same results.heydean

1 Answers

3
votes

Remove this line from your form and try again

.DataBindings.Add("SelectedValue", Me.m_dsBidResults, Company.company_id")

Explanantion:
This code told the combobx that its SelectedValue-property should be bound to the company_id of the dataset. Which is useless, because you already added a list by setting the datasource and you said what the valuemember and displaymember are. Then you implemented your own logic on what it should do when the value changes by using the SelectionChangeCommitted event. The extra line that you removed is only usefull if you have another bound object, say of type Person which has a property that shows in what company he works. In that scenario, when the combobox changes, you want the select company_id to be pushed to the Person-object. Something like

personBindingsource1.DataSource = somePerson;
cboCompany.DataBindings.Add("SelectedValue", personBindingsource1, "WorksAtCompany")

Hopefully it makes some more sense now :)