Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Object Binding Tips and Tricks

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.


advertisement
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.

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 LastName and FirstName properties. With object binding, you don't have to write code to put the value of the LastName property into the Text property of the LastName TextBox and the FirstName property value into the Text property of the FirstName TextBox. And then visa versa when the user changes the text in the controls. Rather, all of this is handled automatically.

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.
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 the tab key 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.

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

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap