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.