Sometimes I need to bind one of the properties from a data source to a control that does not support data binding. Previously I used to do this by adding a hidden control that I could bind to, and then handling the appropriate "changed" event from that control to set the desired property on my non-bindable control.
It would obviously be cleaner to do this with a custom property on my form (instead of the hidden control), but I can only get this to work one way. i.e. the data source is setting my property which is reflected in my control. But if I make changes to the control, it sets the custom property, but this is not reflected back in the data source.
Here’s some sample code:
' Add my custom property to the form's data bindings
Private Sub MyForm_Load(...) Handles Me.Load
Me.DataBindings.Add("CostType", BindingSource, "CostType")
End Sub
' My custom property
Private mCostType As Integer
Public Property CostType() As Integer
Get
Return mCostType
End Get
Set(ByVal value As Integer)
If mCostType <> value Then
MyControl.SelectedPageIndex = Index
mCostType = value
End If
End Set
End Property
Private Sub MyControl_SelectedPageChanged(...) Handles MyControl.SelectedPageChanged
' The user has changed the control so reflect this in the custom property
Me.CostType = MyControl.SelectedPageIndex
End Sub
Am I missing something obvious, or is this not simple to do.