How to perform Binding to a ComboBox in Windows Forms

0

I need to bind to Combobox to automatically save the data in the database.

* I fill Combobox when starting Form :

lPropositosInmueble = BOPropositoInmueble.PropositoInmuebleCollection
    With Me.cboProposito
        .DisplayMember = "Nombre"
        .ValueMember = "PropositoID"
        .DataSource = lPropositosInmueble
        .SelectedIndex = -1
    End With

When I search for the data in the table, I match the Combobox and apply the binding:

bindingPropositoiImueble = New Binding("SelectedValue", lPropositosInmueble, "Nombre", True, DataSourceUpdateMode.OnPropertyChanged, Nothing)
    Me.cboProposito.DataBindings.Add(bindingPropositoiImueble)
    Me.cboProposito.SelectedValue = captacion.PropositoInmuebleID

To save:

If captacion.IsDirty = False Then
   captacion.Update()
End If

captacion is an object (table) already instantiated and that is saving the other data of Textbox in this way:

Me.txtPrecioCLP.DataBindings.Add(New Binding("Text", captacion, "Precio", True, DataSourceUpdateMode.OnValidation, Nothing, "C0"))

The problem is that it does not save what I selected in Combobox when I want to make a change, for the Textbox I do the same and it works perfectly for me.

    
asked by Sergio O Ayala Yañez 14.04.2016 в 10:45
source

1 answer

0

What I notice is that although you define the Binding you are also assigning the SelectedValue from code.

You should remove this line

 Me.cboProposito.SelectedValue = captacion.PropositoInmuebleID

And in the binding define the property

bindingPropositoiImueble = New Binding("SelectedValue", captacion, "PropositoInmuebleID", True, DataSourceUpdateMode.OnPropertyChanged, Nothing)

I do not know why you put "Name" if there goes to the property that is binded to the SelectedValue. The field representing ValueMember should be associated.

Also analyze how in the binding you must assign the captacion object that you used with TextBox.

In summary in object lPropositosInmueble it is correct to use it to load the combo items, but to determine the binding which defines the selection you must use captacion .

    
answered by 14.04.2016 / 11:00
source