download source code The Data Binding capabilities Microsoft has delivered with the .NET platform can be a powerful developer tool. Boiled down to its most simple definition, Data Binding is the act of linking the properties of an application element to specific elements in a graphical presentation element of the application. An example would be linking a property of a custom object, such as the first name on an employee object, to a text box's text property on a windows form.
Data binding is not a new concept for developers. It has been an essential part of any application; it is the most basic way to move information between an application's end users and the processing logic. Many times this has been done by brute force, manually writing code to move values between properties on objects and GUI elements such as text boxes, data grids, and check boxes. What is new, however, is that the .NET Framework provides a rich set of functionality out of the box. Specifically, the framework provides a great deal of motivation to start using data binding within applications:
- A "set and forget" approach to maintaining synchronization between GUI controls and object properties
- Built in type conversion, such as mapping a decimal type property on an object to the "text" string type property on a text box
- A built in system based on delegates and events to "watch" objects for property changes
- Building of rich UI experiences by allowing objects to manage the GUI control properties with minimal developer interaction- such as not enabling a save button until values are changed
- A rich set of events to allow developers to override the default parsing and formatting of values into the assigned control properties
- Built in data type validation of controls, such as ensuring only valid dates are entered in a text box tied to a date time property on an object.
In my experiences, the built-in data binding functionality has not been utilized to its fullest potential. I still see a lot of clients continuing to write large amounts of code to marshal data between the GUI and application objects. Perhaps it is lack of realization of what .NET has to offer or a lack of understanding of how.NET manages data binding behind the scenes. It is my hope to eliminate both of these hurdles with this article and begin to open up the possibilities of data binding to your applications.
I would like to set some boundaries up front of what will and will not be covered in this particular article, as data binding is a broad subject. This article only focuses on using data binding with custom single instance objects binding to Windows Forms controls. I will not be focusing on Web Form binding, nor will I be focusing on the binding of custom collections to elements such as data grids and drop down boxes. I realize that a large amount of development around ASP.NET Web Forms is being completed; however, I chose to focus on windows forms in order to help prepare you for Smart Client development which is very quickly gaining attention and favor in .NET shops.
Background
Throughout the discussions and code samples within this article, I use the same class and Windows Form. My goal was to make the sample code simple so as to not cloud the discussions with unnecessary detail, yet be complicated enough to be of interest. With that, I have chosen a single class, an Employee, and a corresponding form to display the properties of that object in a read - write fashion.
The class I chose for this article is representative of a typical business entity. As such, you will not see any complicated business logic or data access which would reside in other classes in other tiers. When properties need to be populated on the object for purposes of this discussion, I do this manually with hard coded values. When the object needs to be saved, I perform a simple serialization to a file. Again, this is all done to simplify the examples in order to focus on data binding.
The UML representation of the Employee class is shown in Figure 1.

Figure 1
If you wish to view the entire source for the class, please follow the download links at the beginning of this article. There are three solutions included in the download; each corresponds to the various approaches of data binding discussed in the remainder of this article.
There are a few areas of the code to discuss before moving on to data binding. The first is the "dirty flag" and its implementation. The dirty flag indicates if any properties on the object have been changed to a different value. When ever a property on the object is set, a call to the private CheckDirty function is made, as in Figure 2.

Figure 2
The property is not set to the new value unless the call to CheckDirty determines the current value of the property is different from the proposed value.
The logic of CheckDirty appears in Figure 3. In the subsequent "Data Binding 301" section of this article, you will make use of the dirty flag to enhance the UI experience.

Figure 3
The form that will be used throughout the article appears in Figure 4.

Figure 4
The form consists of text boxes to display values such as the first and last name of the employee as well as the salary and hire date. I selected different data types, such as decimals and DateTime so that you can examine how data binding within the Framework handles casts and conversions. I also included a check box for the Boolean isActive property and two buttons, "Save" and "Load New Values".
Now that I have set forth the ground work of what the application elements that you will be working with look like, you will start looking at how data binding works.