title: Binding Combobox to Object Datasource
body: <p>I've got a form bound to an object datasource. It has one text box and one combo box. I set up one binding source for the main object and one binding source for the combo box. When I run the form, the text box is bound correctly, and the list of values in the combo box is bound correctly, but the ValueMember of the combo box isn't working correctly. </p>
  
  <p>The combo box shows the correct list, but it's selected index is 0 instead of what it should be 2. When I change the value in the text box, it's bound object's Property.Set method is called correctly, but the same Property.Set method is not called for the combo box.</p>
  
  <p>I know I can hack up the OnSelectedIndex change methods in the form, but I would like to know what I am doing wrong in just using the Bindings.</p>
  
  <p>Here is the code on the form:</p>
  
  <pre><code>Public Class Form1
  Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
      Dim NameValueBindingSource1 As New BindingSource()
      Dim WorkOrderBindingSource1 As New BindingSource
  
      'Create main object to bind to
      Dim wo As New WorkOrder
      wo.WOIndex = "2012-0111"
      wo.WorkOrderType = 3
  
      'Create list object for combo box
      Dim NameValues As BindingList(Of NameValue)
      NameValues = FillNameValueList()
  
      'Bind Text Box to Binding Source
      WorkOrderBindingSource1.DataSource = wo
      WOIndexTextBox1.DataBindings.Add("Text", WorkOrderBindingSource1, "WOIndex")
  
      'Bind Combo Box to Binding Source
      NameValueBindingSource1.DataSource = NameValues
      WorkOrderTypeCombo.DataSource = NameValueBindingSource1
      WorkOrderTypeCombo.DisplayMember = "Value"
      WorkOrderTypeCombo.ValueMember = "Code"
  
  End Sub
  
  Function FillNameValueList() As BindingList(Of NameValue)
      Dim bl As New BindingList(Of NameValue)
      Dim nv As NameValue
  
      nv = New NameValue
      bl.Add(New NameValue("Short", 0))
      bl.Add(New NameValue("Middle", 1))
      bl.Add(New NameValue("Long", 2))
      bl.Add(New NameValue("Very Long", 3))
  
      Return bl
  End Function
  End Class
  </code></pre>
  
  <p>Here's the code for the main object - "WorkOrder"</p>
  
  <pre><code>Imports System.ComponentModel
  Public Class WorkOrder
      Implements IEditableObject
      Implements INotifyPropertyChanged
  
      Private mWOIndex As String
      Private mWorkOrderType As Integer
  
      Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
  
      Public Property WOIndex As String
          Get
              Return mWOIndex
          End Get
          Set(value As String)
              mWOIndex = value
              RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("WOIndex"))
          End Set
      End Property
      Public Property WorkOrderType As Integer
          Get
              Return mWorkOrderType
          End Get
          Set(value As Integer)
              mWorkOrderType = value
              RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("WorkOrderType"))
          End Set
      End Property
      Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
      End Sub
      Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
      End Sub
      Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
      End Sub
  End Class
  </code></pre>
  
  <p>Here's the code for the object used in the combo box</p>
  
  <pre><code>Imports System.ComponentModel
  
  Public Class NameValue
      Implements IEditableObject
      Implements INotifyPropertyChanged
  
      Private mValue As String
      Private mCode As Integer
  
      Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
  
      Public Property Code As Integer
          Get
              Return mCode
          End Get
          Set(value As Integer)
              mCode = value
              RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Code"))
          End Set
      End Property
      Public Property Value As String
          Get
              Return mValue
          End Get
          Set(value As String)
              mValue = value
              RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Value"))
          End Set
      End Property
  
      Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
      End Sub
      Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
      End Sub
      Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
      End Sub
      Public Sub New(InitValue As String, InitCode As Integer)
          Value = InitValue
          Code = InitCode
      End Sub
  End Class
  </code></pre>