Results 1 to 4 of 4

Thread: [RESOLVED] Databindings for label on a winform usercontrol

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    2

    Resolved [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.

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    2

    Re: Databindings for label on a winform usercontrol

    Quote Originally Posted by MonkOFox View Post
    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.

  4. #4
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width