Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

September 8, 2009

Using Object Binding

Filed under: C#,Data Binding,VB.NET,Visual Studio,WinForms @ 5:23 pm

As stated here, you use object binding in a WinForms application by following these steps:

1. Build the business objects for your application.

2. Define a business object data source in the Windows Application
project containing your user interface.

3. Bind properties of the controls on the form to business object
properties.

Building the Business Objects

The idea with object binding is to bind your business objects to your user interface controls. So the first step is to create the business objects. This example builds a Customer class.

NOTE: Add a Class Library project to your solution and create the Customer class in the Class Library project. You could add the Customer class to your WinForms project, but it is not recommended.

In C#:  

public class Customer
{
    public int CustomerId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string EmailAddress { get; set; }
}

The above code takes advantage of automatically implemented properties. VB does not yet have this feature, but it is expected with VB 10.

In VB:

Public Class Customer

    Private _CustomerId As Integer
    Public Property CustomerId() As Integer
        Get
            Return _CustomerId
        End Get
        Set(ByVal value As Integer)
            _CustomerId = value
        End Set
    End Property

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

    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

    Private _EmailAddress As String
    Public Property EmailAddress () As String
        Get
            Return _EmailAddress
        End Get
        Set(ByVal value As String)
            _EmailAddress = value
        End Set
    End Property
End Class

Configuring a Data Source

In Visual Studio, a data source defines the source of data, such as a business object or a database. To use a data source to bind your user interface, you need to configure the data source for the project containing your user interface, such as your Windows Application project.

For object binding, each data source represents a single business object class. This means that you configure a data source for each business object class that you want to bind. Luckily, this process is quick and easy.

When you use object binding, the data source is referred to as an object data source. This is technically accurate because at runtime, the binding binds to a specific business object. However, this can look a little confusing at design-time because each data source represents a single business object class. The Data Sources window displays each object data source with the class name and lists the public properties of the class. You then bind each property of the class to a control on the form.

To set up an object data source for your user interface:

1. Build your business object Class Library project.
Only compiled business object classes are recognized by the Data
Source Configuration Wizard and the Data Sources window.

2. Select the Windows Application project in Solution Explorer.
NOTE: Always ensure that the Windows Application project is selected in
Solution Explorer before you work with the Data Sources window.

3. Select Data | Show Data Sources from the main menu bar.
The Data Sources window is displayed.

4. Click the Add New Data Source link in the Data Sources window, or click the Add New Data Source button on the Data Sources window toolbar, or select Data | Add New Data Source from the main menu bar.

NOTE: The Add New Data Source link only appears when the Data Sources
window is empty.

This launches the Data Source Configuration Wizard. The first page, shown below, allows you to select the source of the data.

image

5. Select Object for object binding, and click Next.

The second page of the Data Source Configuration Wizard, shown below, provides the list of classes for your selection.

image

The tree view only lists the classes in the Windows Application
project and classes in any component referenced by the Windows
Application project. If you already have a reference to your business object Class Library component, the component appears in the Data Source
Configuration Wizard. Otherwise, you can add a reference using the Add Reference button.

6. Use the tree view to navigate to the class you wish to use for object binding.

image

7. Select the desired class, and click Finish.
The object data source for the selected class is added to the Data
Sources window, as shown below.

image

NOTE: The Data Sources window lists only public class properties.

Use the Data Source Configuration Wizard to define a data source for every business object class you plan to use for binding.

If you are curious about where the Data Sources window stores its information, it is in a set of XML files. Select the Windows Application project in Solution Explorer, and click the Show All Files button on the Solution Explorer toolbar. All the system files for the project are then accessible from Solution Explorer. Open the Properties node (My Project node in VB) under the Windows Application project, and open the DataSources node to see the XML files for the data sources in the Data Sources window.

Creating data sources is quick and easy. Add a data source to the Windows Application project for any business object class that you want to use for binding to your user interface.

Bind the User Interface Controls

There are many ways to bind the resulting object data source to your user interface. These are covered in a later post.

(Based on an except from "Doing Objects in Visual Basic 2005".)

Enjoy!

6 Comments

  1.   Nguyen — October 13, 2009 @ 6:38 pm    Reply

    Good one!

  2.   Ed — June 4, 2010 @ 3:20 pm    Reply

    In the above post you said:

    NOTE: Add a Class Library project to your solution and create the Customer class in the Class Library project. You could add the Customer class to your WinForms project, but it is not recommended.

    Why do you recommend creating a new project for your class library?

  3.   DeborahK — June 4, 2010 @ 6:55 pm    Reply

    Hi Ed –

    The main reason is to keep the UI separate from your business objects. This allows you to reuse the business objects with different user interfaces.

    For example, in one application I worked on we had both a WinForms and WebForms (ASP.NET) user interface working with the same business objects.

    In another application we had both a WinForms and a Silverlight user interface working with the same business objects.

    Hope this helps.

  4.   Atif Shah — June 6, 2010 @ 6:47 am    Reply

    Sir, can you please share one, the most simplified complete winForm and Web application using business objects. That will be a great post

  5.   DeborahK — June 6, 2010 @ 3:31 pm    Reply

    Hi Atif –

    I am a consultant and develop applications for my clients. I regret that I cannot share any of their code.

    I did do a sample WinForms application that you can find here:

    http://www.insteptech.com/techLibrary/samplecode.htm

    Check out the business object example.

    Hope this helps.

  6.   Marta — November 5, 2010 @ 10:08 am    Reply

    Hi Deborah,
    Thank you for sharing your insights. I set up a combo box binding exactly as you instructed above to one of lesser business objects (a). All works perfectly. However I do have a follow up question.

    The overall UI is driven by a main business object (b). How do I set up the data binding of the combo box such that selected item is driven by the property of main business object (b)?

    I greatly appreciate your guidance!
    Thanks
    Marta

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2018 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs