Object Binding Tips and Tricks Gaining the full potential of object binding requires more than just dragging and dropping your properties onto forms. In this article I’ll present a few tricks you need to know to get the most from your object binding. I was one of those many developers who, since Visual Basic 2.0, have hated the concept of data binding. It incorporated patterns that were not extensible, it did not make use of good programming practices, and it frequently didn’t work as expected. But I have to admit, I love object binding. | " | Any time you add a property to the business object, you must rebuild the project before the property will appear in the Data Sources window.
| " |
This article first reviews some object binding fundamentals. It then presents tips and tricks for enhancing your object binding. Once you see all of the things that object binding can do, you may find a new love as well. Object Binding Fundamentals The basic premise of object binding in Windows Forms is to tie Windows Forms control properties, such as the Text property, to your business object properties. When you create a business object and assign values to the properties, the user interface of your application will reflect those values without you needing to write code to copy property values into the controls. And when the user modifies the contents of the controls, the updated values are automatically assigned to the properties of your business object. For example, suppose you have a form that displays customer information and a Customer business object with Last Name and First Name properties. With object binding, you don’t have to write code to put the value of the Last Name property into the Text property of the Last Name TextBox and the First Name property value into the Text property of the First Name TextBox. And then visa versa when the user changes the text in the controls. Rather, all of this is handled automatically. Unlike binding to a database, which generates a significant amount of code and then binds to that generated code, object binding binds to your code. That gives you much more control and greatly simplifies the maintenance of your application. To try out object binding, create a new Windows Forms project and add a class to the project. (In a “real” application, you’d likely put the class into a separate project. This sample adds all of the classes to the Windows Forms project for simplicity.) Then give that class several properties. For example, a Customer class has LastName, FirstName, and CustomerID properties. The LastName property code is as follows. In VB: Private _LastName As String Public Property LastName() As String Get Return _LastName End Get Set(ByVal value As String) _LastName = value End Set End Property
In C#: private string _LastName; public string LastName { get { return _LastName;} set { _LastName = value;} }
The quickest way to create property procedures is to use the property snippet. In a VB code window, type property and then press tab to create a VB Property procedure. In a C# code window, type prop and then press tab twice to create a property. For more information on using snippets or creating your own, see the article “Having Fun with Code Snippets” in the Jan/Feb 2006 issue of CoDe Magazine. After you have defined any desired properties on your business object, build the project. The build process makes your business object accessible to the Data Sources window. Any time you add a property to the business object, you must rebuild the project before the property will appear in the Data Sources window. Next you’ll build the data source. If you don’t already see it, show the Data Sources window by choosing Show Data Sources from the Data window, then click Add New Data Source. This launches the Data Source Configuration Wizard. To create an object binding data source, select Object on the first page of the wizard and the desired business object on the second page of the wizard. Click the Finish button and the new object data source will appear in the Data Sources window. Notice that each of the properties of the object appears below the object name in the window. Now the fun part. Open the default form that was created when you created the project. Then drop down the combo box associated with the business object in the Data Sources window and select DataGridView to display all business objects in a grid or select Details to display each individual business object property as a separate control on the form. Finally, drag the name of the business object from the Data Sources window to the form. Figure 1 shows the result using the Details selection. For more information on building forms using the Data Sources window, see “Drag-Once Databinding” in the September/October 2004 issue of CoDe Magazine.  Figure 1: By default, the BindingNavigator (VCR-like control toolbar) is also added to a form when a business object is dragged to the form. Just delete it if you don’t want it on your form.The object binding data source is automatically added to the component tray of your form and named after the name of your business object, in this case it is CustomerBindingSource. The generated controls are automatically bound to this binding source. The Visual Studio IDE uses the names of your properties to create the labels for the controls and is smart enough to add spaces between words based on underscores or alphabetic casing. This provides a very good starting point for the user interface of your application. | & | | 
By: Deborah Kurata
Deborah Kurata is cofounder of InStep Technologies Inc., a professional consulting firm that focuses on turning your business vision into reality using Microsoft .NET technologies. She has over 15 years of experience in architecting, designing and developing successful applications.
Deborah is the author of several books, including Best Kept Secrets in .NET (Apress), Doing Objects in Visual Basic 6.0 (SAMS) and Doing Web Development: Client-Side Techniques (Apress). She is on the INETA Speaker’s Bureau, is a well-known speaker at technical conferences, and is a Microsoft Most Valuable Professional (MVP). After a hard day of coding and taking care of her family, Deborah enjoys blowing stuff up (on her XBox of course).
Some of the information in this article was obtained from her upcoming book, Doing Objects in VB 2005.
deborahk@insteptech.com | Fast Facts | | Visual Studio 2005 and the .NET Framework 2.0 greatly enhance the data binding story in Windows Forms, This article explores the classes, interfaces and coding techniques you can use today in your Windows Forms applications. | |
|