Data binding problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello. I'm having trouble getting a combobox bound to a property of an
object. I am populating the combobox using data binding, but I can't
get the SelectedValue property to bind to an object; it never gets
updated. I've included my short sample below, it's from a form with just one
control, a combobox named "uxChoice". The line of code giving me grief
is: uxChoice.DataBindings.Add("SelectedValue", model, "SomeVariable")

Public Class Form1
Public Class anEntry
Public Sub New(ByVal objectId As Integer, ByVal description As
String)
_objectId = objectId
_description = description
End Sub

Private _objectId As Integer
Public ReadOnly Property ObjectId() As Integer
Get
Return _objectId
End Get
End Property

Private _description As String
Public ReadOnly Property Description() As String
Get
Return _description
End Get
End Property
End Class

Private listOfChoices As anEntry() = {New anEntry(1, "A"), New
anEntry(2, "B"), New anEntry(3, "C")}

Public Class SomeModel
Private _someVariable As Integer
Public Property SomeVariable() As Integer
Get
Return _someVariable
End Get
Set(ByVal value As Integer)
_someVariable = value
End Set
End Property
End Class
Private model As New SomeModel

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
uxChoice.DataSource = listOfChoices
uxChoice.DisplayMember = "Description"
uxChoice.ValueMember = "ObjectId"
uxChoice.DataBindings.Add("SelectedValue", model,
"SomeVariable")
End Sub

Private Sub uxChoice_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
uxChoice.SelectedIndexChanged
If uxChoice.SelectedValue IsNot Nothing Then
MsgBox(uxChoice.SelectedValue.ToString)
End If
MsgBox(model.SomeVariable.ToString)
End Sub
End Class
 
Remove the databindings line, you don't need it after setting the
datasource manually. Use one or the other, not both. Also it is
preferred to use the SelectionChangeCommitted(ByVal sender As Object,
ByVal e As System.EventArgs) event with a combobox control, assuming
you are using .net 2.0
 
The datasource is used to create the list of choices in the combobox; the
databindings line is to bind the selected value to a property of an object,
two separate things, I don't see how removing the databindings line is going
to help anything.
---scott
 
The datasource is used to create the list of choices in the combobox; the
databindings line is to bind the selected value to a property of an object,
two separate things, I don't see how removing the databindings line is going
to help anything.
---scott





- Show quoted text -

To bind the selected value to another control, use the second
controls .DataBindings Collection

If you are binding the selected value of one combobox to a textbox
name MyTextBox

MyTextbox.DataBindings.Add("Text", uxChoice, "SelectedValue")
 
I'm not trying to bind to another control; as I mentioned earlier I'm trying
to bind to the property of an object, see the example.
---scott
 
Sorry for the misunderstanding.

The DataBindings collection of a control will only bind that controls
properties to that of another object, it doesn't work in reverse. In
order to do this the way you would like, you would have to create a
bindable class either by implementing IBindableComponent or something
similar. The simplest solution would be to set the model instances
properties in the SelectionChangeCommited event. Whether or not that
will work for you depends on your ultimate goal.

Here is some ref to the interface

http://msdn2.microsoft.com/en-us/library/system.windows.forms.ibindablecomponent.aspx
 
Back
Top