While the new DataGridView has a lot more functionality than the Visual Studio 2003 DataGrid, you may want to display your object with individual controls. Let's change the CustomerDataGridView to Dock=Top
and then leave some room at the bottom of the form.
|In order to enable Drag Once Databinding and "Connect the Dots," the Data Sources Window needs to know which property should be bound on each control. To determine which property should be auto-bound, the VB team added 3 new attributes to System.ComponentModel.|
- Use the DefaultBindingProperty for simple controls like the TextBox.
- Use the ComplexBindingProperties for form controls that manage a list like the DataGridView.
- Use the LookupBindingProperties for controls that manage a list as well as an individually bound item such as ComboBox.
Within the Data Sources Window, select the Customer object and change the DropType
by selecting the dropdown list next to the Customer node in the TreeView. You can modify any of the DropTypes
for the individual properties as well, including your own custom controls.
Change the DropType
to a Label. With the Customer DropType
set to Details
, drag it from the Data Sources Window and drop it on the form below the CustomerDataGridView. As a result you'll see controls for each public property created. Each control is databound to the selected property and a label is provided.
As Columbus discovered, the world isn't flat. Neither are our object models. Many developers prefer objects because they can better define their data hierarchies. They may have Order, OrderItem, and Product objects. They may even have Address objects they wish to represent in their object model.
|Generics allow us to define the "shape" of an object within a list. So, rather then return a collection of Objects which you would then have to cast back to your Order object, you can return a collection of Order objects.|
Let's take a look at how Visual Studio 2005 takes this into consideration. First let's create a basic Order object and look at its properties. Later you'll build upon this. Add the code in Listing 2
to your Business class library.
Now that you have your Order object you need to add it to your Customer object. What you really want is something that looks at the Customer object to see a collection of strongly typed Orders. In previous versions of Visual Studio you might have created your own implementation of a collection. However, Visual Studio 2005 VB, C#, and C++ with managed extensions will support a concept referred to as generics
. The .NET Framework 2.0 has several collections that leverage generics. The basic collection would be:
T represents the type you want the collection to return.
Dim _orders as Collection(Of Business.Order)
In the Customer.Orders example you'll want to leverage some of the databinding infrastructure to handle deleting or adding new objects. For instance, when the end user clicks to create a new OrderItem you want to default the new OrderItem to have its OrderId
set to the parent. You should place this code within the Order object rather then the form it's instanced on. To leverage these events the.NET Framework 2.0 includes a new BindingList that leverages generics.
You will expose the Orders as a property on the Customer object.
To minimize the amount of code you must type each time, add an Imports statement to the top of the class:
Then, add a property for the Order object:
Private WithEvents _orders As _
Public Property Orders() As _
Set(ByVal value As _
_orders = value
At this point let's see what this looks like. Build the class library and then activate Form1
in the Form Designer. With Form1
active in the designer, activate the Data Sources Window. If you've built the Business class library, the Data Sources Window will now display your Customer with a collection of orders, which is also expandable.
|Author's Note: Due to a bug in Beta 1, you may get an error and need to close Form1, rebuild, and then reopen it.
Change the DropType
of the Orders property to DataGridView and drag it to the form below the Customer details. To make the form a little more manageable, use the Property Grid to set OrdersDataGridView.Dock = Bottom
If you look in the component tray you'll notice a new DataConnector was created named OrdersDataConnector
. Looking at the code you'll see the OrderDataConnector is linked through the CustomerDataConnector.
Me.OrdersDataConnector.DataSource = _
Me.OrdersDataConnector.DataMember = "Orders"
This means the OrdersDataConnector
will provide a list of orders for the Current item in the Customer DataConnector.