RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Object Binding Tips and Tricks : Page 2

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.

Populating Your Business Objects
If you run your application at this point, you will see that it does not do anything. Even though the controls are bound to the properties, the properties have no values. So the next big step in object binding is populating the business objects.

Object binding to a class that inherits from BindingList(Of T) provides a very nice way to display and manage lists of things.
You can retrieve data and populate your business objects in many different ways. You could use a DataSet or DataTable; you could set up a DataReader; you could call a Web service to retrieve the needed data; or you could use TableAdapters to further bind your properties to database fields.

To keep this example simple, a private Retrieve method on the business object pretends to retrieve the needed data. Actually, it uses ADO.NET to create a DataTable in code. That way you don't need to create a database, stored procedures, and/or Web service in order to test out object binding. Listing 1 shows the VB code for the Retrieve method; Listing 2 shows the Retrieve method in C#.

Best practices dictate using the factory pattern to create the business objects and populate them from the retrieved data. You could choose a number of ways to implement the factory pattern. In this example, the factory pattern implementation makes the constructor private (so no other code can create the object) and defines a static (shared) Create method that creates and populates the instance as shown.

In VB:

   Private Sub New()
   ' Don't allow creation of this object
   End Sub
   Public Shared Function Create(ByVal CustomerID _
      As Int32) As Customer
      ' Create the customer
      Dim oCustomer As New Customer
      ' Retrieve the data for this customer
      Dim dr As DataRow
      dr = oCustomer.Retrieve(CustomerID)
      ' Populate the business object
      Return oCustomer
   End Function
In C#:

   private Customer()
      // Don't allow creation of this object 
   public static Customer Create(Int32 CustomerID)
      // Create the customer
      Customer oCustomer = new Customer();
      // Retrieve the data for this customer
      DataRow dr =oCustomer.Retrieve(CustomerID);
      // Populate the business object
      return oCustomer;
The method that populates the business objects is as follows.

In VB:

   Private Sub Populate(ByVal CustomerDataRow As _
      With CustomerDataRow
         Me.LastName = .Item("LastName").ToString
         Me.FirstName = .Item("FirstName").ToString
         Me.CustomerID = .Item("CustomerID").ToString
      End With
   End Sub
In C#:

   private void Populate(DataRow CustomerDataRow)
      this.LastName = 
      this.FirstName = 
      this.CustomerID = 
You would need to enhance the code in the Populate method each time you add a property to the business object. If you use a TableAdapter or typed DataSet to retrieve the data instead of using an untyped DataSet, DataReader, or some other technique, you would reference the data columns by their automatically generated strongly typed names instead of their quoted string column names.

To complete the object binding, add code in the Load event for the form to create and bind the object. Call the Create method to create the instance and then assign the CustomerBindingSource to that instance to bind it as follows:

In VB:

   Private Sub CustomerWin_Load(ByVal sender As _
      Object, ByVal e As System.EventArgs) _
      Handles Me.Load
      Dim CustomerInstance As Customer
         CustomerInstance = Customer.Create(1)
         CustomerBindingSource.DataSource = 
   End Sub
In C#:

   private void CustomerWin_Load(object sender,
      EventArgs e)
      Customer CustomerInstance = 
      customerBindingSource.DataSource = 
At this point you can run the application. If all is well, the form should appear with the property values assigned as the text of the controls.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date