Simple Control Binding to objects

Ok, I feel like an idiot but I cannot get simple control binding to work. I've set up  a DataSource and mapped it to my custom object. Assigned the DataSource and the Databinding to several of the properties of the object on the actual control.

But when I run the form - no databinding.

What am I missing Apparently there needs to be something to trigger the binding, but looking through the method interface there appears to be nothing there...

Also, anybody have a link to where databinding basics are described for Winforms 2.0 I've searched around and there are all sorts of articles that talk about list binding and using the navigator control. I'm looking for something more basic that talks just about simple control binding and manually updating the control sources.


+++ Rick ---  

p.s.
First impression of this stuff - if you need to read docs to figure out databinding it's too freaking complex. Databinding should be easy and obvious and nearly transparent - the process just to hook up these binding sources is still a major pain in the ***... I'm trying to evaluate whether I should use the built in binding or continue to use my home grown implementation which works well. I'd rather use built-in, but it's gotta provide at least the same ease of use and functionality.                


Answer this question

Simple Control Binding to objects

  • 3d_developer

    Rick
     - How did you set up the bindings Did you use the Datasources Windows (see http://msdn2.microsoft.com/en-us/library/6ckyxa83.aspx)

     - What does the object you are binding to look like - are you implementing change events

     - How do you populate the objects you are binding to

    Let me know 

    mark 


  • RajLakamana

    Hi Rene,

    This is about as simple as it gets. But I'm not binding to data but to an object in the same project. No problem setting up the data source and attaching it to the BindingSource and then to the control.

    But it's not binding. Something's missing to initiate the binding I'm guessing, but I have no idea what...

    At this point I'm back to my old binding classes...

    +++ Rick ---   


  • Kevin F Jones

    Mark,

    At this point I'm just trying to get inbound binding to work.

    I have an object that's already populated through code. It's a plain POCO object with public properties and private fields and I'm binding to a couple of properties.

    Maybe I'm totally not getting how this databinding is supposed to work. What I need is to have my code populate the objects (and update them back to the database) - I want the databinding to manage the mapping of control to properties - ie. plain and simple Simple Control binding.

    I'm not really interested in having some manager object that fetches data for me <g> - I just want to bind to what's already there and keep the object in sync with what's in the controls.

    I've set up a DataSource to this object and bound the Binding Source to it, then use the binding source on the control. In the designer everything seems to work OK.

    I was assuming that's all that should be needed for a control to bind - a DataBinding set to the bindingSource and a specific property of the object.

    It looks to me that's not enough, since the data isn't displaying Something needs to trigger the binding

    What makes the the form/controls start binding in this scenario

    I'm looking through the links you've pointed me at (thanks) but all of that binds to datasource against a database which necessarily works a little differently.


    +++ Rick ---




  • Ritesh Mehta306937

    Hi Rick.

    In what control you can't do the databinding and you custom object is Database or an object (a class in a dll)

    I had problems with the databinding in textbox and DataGridView Controls, but I can fixed with a few line of code, but tell me more about you project may be I can help you.

    RomanSmile

  • elamathi

  • sniwas24x7

    If the object does not raise change notification then you will need to either set the populated object into the BindingSource or tell the BindingSource to refresh its controls (ResetCurrentItem).

    I wrote a simple example that does this by doing the following:

    1. Created a Customer class (see end of post)
    2. Built project
    3. Opened the Data Sources Window using the menu Data->Show Data Sources
    4. Selected Add New Data Source...
    5. Selected "Object"
    6. Expanded my project to find Customer and selected it and finished the wizard
    7. In the DataSources Window, Selected Customer
    8. Clicked on the Dropdown next to it and selected "Details View"
    9. Drag and Dropped Customer onto the Form
    10. This created a couple of Labels, bound TextBoxes and a CustomerBindingNavigator and CustomerBindingSource
    11. Double Clicked on the Form and added this to the Load event:

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
               CustomerBindingSource.DataSource =
    New Customer("Mark", "Boulter")
        End Sub

    12. Ran the Form - the TextBoxes showed the data from the Customer object
    13. Went back to the Form and added 2 buttons.
    14. In the first Button updated the Customer object

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim c As Customer = CustomerBindingSource.DataSource
            c.LastName =
    "Jones"
        End Sub

    15. In the second Button refreshed the BindingSource:

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            CustomerBindingSource.ResetCurrentItem()
        End Sub

    16. Ran the Form - when I click on the first button the object is updated but the UI does not update. When I click on the second button the UI does update.

    Hope this helps. If it does not let me know.

    mark

    P.S. I would not have had to add the ResetCurrentItem if I had implemented change notification on the Customer object.

    Customer object:

    Public Class Customer

        Private _lastName As String
        Private _firstName As String

        Public Sub New(ByVal newFirstName As String, ByVal newLastName As String)
            _firstName = newFirstName
            _lastName = newLastName
        End Sub

        Public Property FirstName() As String
            Get
                Return _firstName
            End Get
            Set(ByVal value As String)
                _firstName = value
            End Set
        End Property

        Public Property LastName() As String
            Get
                Return _lastName
            End Get
            Set(ByVal value As String)
                _lastName = value
            End Set
        End Property

    End Class

     



  • Alx_it

    I had the same Problems. Everything seemed easy, but it didn't work. But after a few hours I finally found out what the reason is. To prevent you spending this time here's what i found out:

    Using the designer you don't set the "real" DataSource. You only define which type an object must be of to be able to bind. There's a 'typeof' in the Designer.cs!

    this.myBindingSource.DataSource = typeof(myClass);

    What you ALWAYS have to do ADDITIONALLY is to bind an instance of your object to the bindingsource by code like that:

    <BindingSourceName>.DataSource = <myClassInstanceName>

    Suddenly everything's easy again ;) And rather logical, if one thinks about it!

    Happy coding!


  • Simple Control Binding to objects