Browse DevX
Sign up for e-mail newsletters from DevX


Dig In to Drag-once Data Binding : Page 5

Visual Studio 2005 offers productivity features that will reduce the tedium of building forms. Learn how the Data Sources Window will help you build forms faster.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Loading Customers in Form1
Now that you've databound Form1 to the Customer and Order objects, let's get some data. To keep things relatively concise you'll use a CustomerFactory and OrdersFactory set of classes. They will expose a couple of shared/static methods that will return a bunch of stubbed out customers with orders.

To return a collection of customers let's add a new class to the Business class library named CustomerFactory. See Listing 3 for the Customer Factory code.

We'll need a similar object for Orders. Add a new class named OrdersFactory and add the code in Listing 4.

Loading the Form with Data
When you use the Database Data Source type a typed DataSet is created along with a typed DataAdapter. When you drag it to your form, the Data Sources Window will generate a line of code that auto-loads the selected DataTable within the DataSet using the new TableAdapters. To change the default label for each column of a DataTable, change the Caption property on the column within the DataSet Designer.
With the CustomerFactory and OrdersFactory added you're ready to load some data. When you created the Customer and Order controls for Form1 you configured the CustomerDataConnector and OrdersDataConnector to have their "shape" by setting the DataSource to the Customer and Order types. Now you want to get some actual customers. It would be nice if Visual Studio 2005 auto-loaded your objects, however, the VB Data Team didn't want to make any assumptions or enforce a specific design pattern so you'll need to load the DataConnectors with your own pattern. See the sidebar, "AutoLoad Data." You can auto-load your objects in one of two ways: the quick way, and the more usable way.

The quick way:

Private Sub Form1_Load(... Me.CustomerDataConnector.DataSource = _ Business.CustomerFactory.GetAllCustomers() End Sub

This takes the generic BindingList(Of Customer) returned by the CustomerFactory and puts it directly into the DataConnector. This works, but this approach makes it a little more difficult to write code to get it out of the DataConnector. To get the current item from the CustomerDataConnector you would need to write the following code:

Dim selectedCustomer As Business.Customer selectedCustomer = _ CType(Me.CustomerDataConnector.Current, _ Business.Customer)

If your solution does not write a lot of code in the form, that's fine, but let me show you a cleaner approach where you declare a form scoped variable to hold onto the Customers.

Private _customersBindingList As BindingList(Of _ Business.Customer)

Don't forget to add the imports:

Imports System.ComponentModel.Collections.Generic

In the Form.Load event set the variable and assign it to the DataConnector.

_customersBindingList = _ Business.CustomerFactory.GetAllCustomers() Me.CustomerDataConnector.DataSource = _ _customersBindingList

This makes it a little easier to grab the current item. You don't have to always cast the DataConnector.Current; you can just use the Position property.

' Get the current Customer Dim selectedCustomer As Business.Customer selectedCustomer = _ _customersBindingList(_ Me.CustomerDataConnector.Position) ' Get the CustomerId from the current Customer Dim selectedCustomerId As String selectedCustomerId = _ _customersBindingList( _ Me.CustomerDataConnector.Position).CustomerID

Use either one of the above methods and press F5. You should now have customers loaded in your form. As you navigate through customers, the list of orders is displayed for the selected customer.

Comment and Contribute






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



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