WInforms Combobox SelectionChangeCommitted event does not always change SelectedValue

I have a WinForms application built with VB.Net in VS 2010 and I am scratching my head over the following issue.

I have a form with a combobox that binds to a data source 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 am using the cboCompany.SelectionChangeCommitted event to filter the datagridview data 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 unless the user switches focus to some other control and then returns to the callout. After focus switching, if the user then toggles the combo box to the first item in the combo box (SelectedIndex = 0), the SelectionChangeCommitted event fires, but the SelectedValue remains set to the previously selected value. I verified this by adding a message box to the above event handler showing 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 have verified that the table I am linking to contains unique values ​​for company_id and company_name.

Do I need to use some other event to make sure the SelectedValue has actually changed? Alternatively, ideas for a reliable solution to the problem would be welcome.

+3


source to share


1 answer


Please remove this line from the form and try again

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

      

Explanantion:
This code told combobx that its SelectedValue property should be bound to the company_id of the dataset. This is useless because you have already added the list by setting the datasource and you said what valuemember and displaymember are. Then, you implemented your own logic of what it should do when the value changes using the SelectionChangeCommitted event. The extra line you removed is only useful if you have another associated object, such as Person, which has a property that indicates which company it works for. In this case, when the combobox value changes, you want the select company_id to be clicked on the Person object. Something like



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

      

Hope it makes sense now :)

+2


source







All Articles