[RESOLVED] Databindings for label on a winform usercontrol
I'm creating a usercontrol for a winform app that contains two labels, one as a header and the other one needs to bind to a datasource through Me.usercontrol1.databindings.add(). I'm a novice in usercontrol designing so I searched on internet to find how to make a databindings for my control. I realized that I need to use ControlBindingsCollection but I don't know exactly how.
I found following codes and added to my usercontrol :
Code:
Private bindingContext_ As BindingContext
Private dataBindings_ As ControlBindingsCollection
Public Overrides Property BindingContext() As BindingContext
Get
If bindingContext_ Is Nothing Then
bindingContext_ = New BindingContext()
End If
Return bindingContext_
End Get
Set(ByVal value As BindingContext)
bindingContext_ = value
End Set
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public Overloads ReadOnly Property DataBindings() As ControlBindingsCollection
Get
If dataBindings_ Is Nothing Then
dataBindings_ = New ControlBindingsCollection(Me)
End If
Return dataBindings_
End Get
End Property
Now I can set usercontrol1.databindings parameters but there's obviously something missing because I need to connect a single returning value from this binding to label2.Text in my usercontrol and I don't know how.
Is there anybody to help me solve my problem ?
Thanks in advance.
Re: Databindings for label on a winform usercontrol
If all your binding is data members from one database row, then just bind the controls you need to be bound:
Code:
Private _data as DataRow
Public Sub New(ID as integer)
InitializeComponent()
' set the me._data to the data returned from the database table using ID
Me.Label1.DataBindings.Add(New Binding("Text", me._data, "Title"))
Me.Label2.DataBindings.Add(New Binding("Text", me._data, "Value"))
End If
If you need to browse through the rows, look into the BindingNavigator.
Justin
Re: Databindings for label on a winform usercontrol
Quote:
Originally Posted by
MonkOFox
If all your binding is data members from one database row, then just bind the controls you need to be bound:
Code:
Private _data as DataRow
Public Sub New(ID as integer)
InitializeComponent()
' set the me._data to the data returned from the database table using ID
Me.Label1.DataBindings.Add(New Binding("Text", me._data, "Title"))
Me.Label2.DataBindings.Add(New Binding("Text", me._data, "Value"))
End If
If you need to browse through the rows, look into the BindingNavigator.
Justin
Dear MonkOFox,
It worked perfectly.
Thanks again.
Re: Databindings for label on a winform usercontrol
That's great! If you don't mind, use the thread tools to mark it resolved. : )
Justin