So know you know all about CurrentManager and BindingContext from the links in the previous blog http://blogs.interknowlogy.com/adamcalderon/archive/2005/05/15/151.aspx and you have a pretty good understanding of what is going on when you do bind to a control like this
TextBox1.DataBindings.Add(“Text”,MyBusinessObject,”CustomerName”). Now it’s time to focus on the business object.
If you bind a control to a business object like above you get one-way binding meaning if you enter data into the Textbox or code something like this TextBox1.Text = “Something” the “set” method of the CustomerName property will be called and the data will be assigned in the business object. If you want two-way binding meaning you can do something like this MyBusinessObject.CustomerName = “Something” and have the textbox update with the value “Something” you have to add a public event to the business object CustomerNameChanged. The pattern here is to append “Changed” to the end of the property name and declare the event of type EventHander. The .NET framework listens for this event to be raised and updates the form control for you creating the two-way binding. The sample class below shows you what you need to do to create a class. Notice a few things highlighted by comments in the class. First you should have strings set to String.Empty or “” because the databinding will throw an exception if you try to bind to nothing. Second notice that you have to declare the public event as stated above. Finally you have to raise this event when the value changes. Raising the event triggers the binding and the form control will not update unless you have this in your set method.
Public Class CCustomer
”’ Setting the string to empty is required for cases were the value has not
”’ been initialized and you try to bind to it
Private _customerName As String = String.Empty
”’ These events are used by databinding to keep the objects and the form controls
”’ in sync.
Public Event CustomerNameChanged As EventHandler
Public Sub New()
Public Property CustomerName() As String
Set(ByVal value As String)
_customerName = value
RaiseEvent CustomerNameChangedEvent(Me, New EventArgs())
So now we know how to bind a form control to a business object and how to code the business object to work with data binding. Next we will work on the business rules and how to connect them to the UI layer.