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
 

Dig In to Drag-once Data Binding : Page 6

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.


advertisement
Linked Forms
Now let's suppose you want to display the order on a separate form. On the first form you'll display a list of customers and a list of orders for each customer within a DataGridView. Let's say you want to show the selected order with OrderDetails on another form. Using the CurrentItem property of the DataConnector you can easily pass this information between two forms.

You'll start by adding a new form to your exe project named OrderForm. You'll add the Order controls to the form and then expose a property to set the current order displayed on the form.

First let's leverage the Drag Once databinding features of the Data Sources Window. With the OrderForm active in the Form Designer, activate the Data Sources Window. Expand the Customer object to see the Orders object. Change the DropType of the Orders property to Details.

To get an overview of all the controls on the form you can use the Document Outline. From the View menu, select Other Windows and then choose Document Outline. (see Figure 5)
You also want to create the customer information for this particular order. Notice how the Orders object has the Customer property. If you look at the DropTypes available, you'll notice you can only see Details and None. The Data Sources Window can tell that this isn't a collection but rather a single instance. Let's change the Orders.Customer DropType to Details. Now drag the Orders property to the OrderForm. Notice how you only have one DataConnector for the OrdersDataConnector. Visual Studio is smart enough to know that there will only ever be one Customer object for the OrderDataConnector, so it doesn't create another DataConnector. If you drag just the customer node from the Data Sources Window a second time, you'll notice Visual Studio 2005 does create a CustomerDataConnector. This is just a bug. After Beta1 Visual Studio will reuse DataConnectors configured to the same node in the Data Sources Window. For this form you'll only show one Order at a time so you can simply delete the DataNavigator. Notice how deleting the DataNavigator has no affect on the databinding. Since the controls are created, databound, labeled, and named, all you have to do is move the controls around to your own specific tastes.

Now let's expose a way to set which order the form will display. Within the order form, double click to go to code view. We'll expose a public property that will represent the Order object of the form. In the Setter, we'll also set the OrderDataConnector to the specific Order.

Private _order As Business.Order Public Property Order() As Business.Order Get Return _order End Get Set(ByVal value As Business.Order) _order = value Me.OrderDataConnector.DataSource = _order End Set End Property

Notice how the DataConnector.DataSource can work with different types of values. The following are all valid:

DataConnector.DataSource = _ GetType(Business.Customer) DataConnector.DataSource = _ New BindingList(Of Business.Order) DataConnector.DataSource = New Business.Order()

The first line sets the "shape" of the DataConnector. Internally the DataConnector maintains a BindingList(Of T). The second line tells the DataConnector to use this new list as its internal BindingList(Of T). The third line tells the DataConnector to use its internal list but set the shape to that of the item being added.

Go back to Form1. You want to capture the selected item and pass it to the OrderForm. Since you know the OrdersDataConnector manages list of bindings for the Order you can leverage its Current property. Then you'll want to pass it to the OrdersForm. In the DoubleClick event of the OrdersDataGridView, add the following code:



My.Forms.OrderForm.Order = _ CType(Me.OrdersDataConnector.Current, _ Business.Order) My.Forms.OrderForm.Show()

Linked Forms in Action
Press F5 to see how this all works. Form1 is now loaded with the two customers. Navigating through the two customers in the DataGridView or the DataNavigator you'll see the individual controls and the OrdersDataGridView automatically refresh. You can then double-click one of the orders and open the OrderForm.

 
Figure 5: Document Outline.
The OrderForm displays the order with the Customer sub-object. By switching back to Form1 and double-clicking another order, the same OrderForm is updated.

Wrapping Up
The new features of Visual studio 2005 continue to enable developers to focus on the business code they need to write. Having to manually create labels, name controls, and bind them is a tedious, time-consuming step that adds little value to your end product. You've seen how Microsoft is opening the doors that limit the design patterns required to support the designer features. You no longer need to add interfaces or inherit from FX base classes just to get designer support. Visual Studio 2005 continues to build on developer productivity. Please contact us with your feedback.

You can use the Data Sources Window for any "Client" project. This includes traditional exe projects including Windows Forms, Device Projects using the Compact Framework, and Word and Excel applications built with Visual Studio Tools for Office.



Steve Lasker is a Program Manager on the Visual Basic team at Microsoft. His team is responsible for many of the Data Design Time features for building client applications including the Data Sources Window and the DataSet Designer. Prior to joining Microsoft, Steve was a technical architect at a large consulting company building Web, client, and device applications for corporate customers. Steve has an engineering background in the remote broadcasting industry. Give his team some feedback at the VB team blog.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap