Login | Register   
LinkedIn
Google+
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 3

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
Business Object Example
 
Figure 2: Object data source type.
Without getting into religious battles over specific design patterns, let's look at a common yet simple business object. In Listing 1 I've defined a Customer object with two properties, CustomerId and CompanyName. I've also disabled the default constructor and I require the CustomerId to instance a Customer object. While this private constructor isn't necessary because the compiler will remove the default parameterless constructor when a constructor with parameters is provided, I've added it for clarity. If I ever make a mistake and remove the parameterized constructor, the compiler will remember that I didn't intend for this class to be created directly.

Adding Objects to the Data Sources Window
Since we only require public properties, it's difficult for the Data Sources Window to know what to display. Using the Add New Data Source command, developers can simply add their objects to the window. Figure 2 and Figure 3 show some of the steps for adding the Customer object to the Data Sources Window.

 
Figure 3: Choosing the Customer Type.
Giving It a Test Drive
Let's walk through an example. For the purposes of this example you'll use the Object data source type. This example uses Visual Basic, but all of this applies just as easily to C#, C++, and J#.

You first want to create a simple hierarchy of a Customer with a collection of Orders. Each Order will have a collection of OrderItems (see Figure 4)

To begin, create a new Windows Forms project and name it DragOnceDataBinding. To follow a typical solution, create your Customer object in a separate assembly. From the File menu, select Add New Project, then select Class Library and name it DragOnceDataBinding.Business. See Sidebar 1 for help resolving the namespace. Within the class library, add a new Customer class. To demonstrate some default behavior of the DataConnector, keep the public constructor.

Let's see what this looks like in the Data Sources Window. Build the entire solution and then select the exe project.

 
Figure 4: Customer in the Data Sources Window.
Open Form1 in the Designer then open the Data Sources Window. If it's not visibly docked with the solution explorer, you can activate it by selecting Show Data Sources Window from the top level Data menu. In the Data Sources Window select Add New Data Source. Within the wizard, select Object as the data source type. You'll now see what appears to be a mini class viewer displaying each assembly and the classes within their namespaces. If your class library assembly isn't listed you probably didn't add the reference. No problem. You can add it directly within the wizard with the Add Reference button.

Now you should be able to expand the DragOnceDataBinding.Business.dll and find your Customer object. If you still don't see your class, you probably just need to rebuild your solution. If you added some XML comments on the Customer class you should see them in the bottom of the wizard. Select the Customer object and finish the wizard.

You should now see your Customer object displayed in the Data Sources Window. If you expand the Customer object you'll see each property with a default control. The icons should look familiar and you'll notice that the DateCreated property is defaulted to a DateTimePicker. You may also notice that the selected node in the TreeView has a drop down. For the root Customer object you'll see DataGridView, Details, and None. Set the Customer DropType to DataGridView then drag the Customer node to your form. This will create a DataGridView that is indirectly databound to the Customer type. To get a quick, VB-like feel for this, let's run the project.

Hit F5 and notice the grid displays with the columns of the Customer object. You'll also notice that by default a new row was added. If you've used the code sample from above, the CustomerId has a new GUID by default. When the DataConnector receives a request to instance a new item, it will look to see if the type within the list has a parameterless default constructor. Let's look at what the Visual Studio Designer did for you behind the scenes.

In order to make this work, Visual Studio created a CustomerDataConnector class. The DataConnector supports binding to types. This means that Visual Studio can look at a type and get its public properties without actually instantiating the object on the form designer. The equivalent of doing this in code would be:

Me.CustomerDataConnector.DataSource = _ GetType(DragOnceDataBinding.Business.Customer)

The DataGridView is bound to the Customer object through the DataConnector with the following line of code:

Me.CustomerDataGridView.DataSource = _ Me.CustomerDataConnector

The DataConnector will then represent the bindable items as any public property of Customer. As you'll see later, this would include any sub list of Orders.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap