Drag Once DataBinding with Custom Controls
Visual Studio 2005 will ship some great new controls, but suppose you want your own control to play in the Data Sources Window? In this article, I'll explain how the Data Sources Window in Visual Studio 2005 will enable you to extend the list of controls supported for Drag Once Databinding. I'll show you how to create a PhoneBox control and an AddressBlock UserControl.
"I don't know what I don't know."
In the September/October 2004 issue of CoDe, I discussed how developers can use Windows Forms to create a Data Bound form with a single drag & drop action. With Visual Studio 2005, this works on all our client platforms including Windows Forms, Visual Studio Tools for Office, Devices, and even Microsoft Reports using Reporting Services. In this article, I'll explain how you can use Drag Once DataBinding with your own set of controls.
First, a Quick Recap
Using the new Data Sources Window, developers can add several different types of Data Sources including databases using typed DataSets, business objects, and XML Web services. The Data Sources Window displays the "schema" of these objects. If you have a DataSet with a list of DataTables, you can navigate through the DataSet and drag your tables or columns to your form. If you have an object with public properties, you can drag away. As a result, Visual Studio creates the specific controls and labels; named and databound. Simply hit F5, and away you go.
For more detail, you can see the full article online at www.code-magazine.com/Article.aspx?QuickID=0409051
So now that you've got the basic idea down, and you've played with all the new controls that ship with Visual Studio 2005, you want to plug in your own control. How does this all work?
Customizing the Controls
To get a feel for how the model works, let's look at customizing the controls that we'll be shipping in the box.
Setting Up a Data Source
Use VS 2005 to create a new Windows Forms project. It doesn't really matter whether you choose VB, C#, J#, or even C++. Open the Data Sources Window and choose Add New Data Source. Choose Database to start with. (This article will cover objects a bit later). It doesn't really matter what database you use, but I'll use Northwind.
Once you provide a connection, you'll be prompted for a connection string name. If you're using Beta 1, be sure to check both checkboxes to save your connection string. We've changed this a bit in Beta 2 to be more intuitive, which hopefully doesn't need explanation, but that's what feedback is all about. (See sidebar, MSDN Feedback Center). Choose one or more tables from the Database Objects step of the wizard. I've chosen Employees, Orders, and Products because they have columns with several data types, which will be helpful for this example.
In the Data Sources Window, expand some of the tables to see the list of columns. Under the Orders table you can see some controls mapped to DateTimePicker, and Photo defaults to none. Test question: Why does the Northwind Employees.Photo column default to None as opposed to a Picture Box? Answer: The column in the dataset comes through as a Byte Array. We don't actually know if this is an image, Word document, or the binaries for Halo® 2. You can see that Products.Discontinued defaults to CheckBox, but if you click the DropDown you may notice a TextBox available as well.
Let's say you want to limit Boolean data types to CheckBox and you know that most, if not all, of your applications use binary data as Photo. You want to "customize" the list, and change the defaults.
Changing the Controls
In the Data Sources Window, select any of the nodes in the TreeView. You'll see the list of controls associated with that data type (Figure 1). For Employee.Photo you can see the list of controls associated with Byte Array. For Products.Discontinued, you see controls associated with Boolean. Choose Customize to launch the Tools-Options dialog box. Note that you're not necessarily customizing just one of these columns. This is just a shortcut to the Tools-Options pane (Figure 2).
Figure 1: Selecting Customize from the Data Sources Window.
Figure 2: Data UI customizations.
By: Steve Lasker
Steve 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 Source Window and the DataSet Designer. Prior to joining Microsoft, Steve was a technical architect and Director of R&D at a large consulting company building Web, client and device applications for corporate customers. Steve has an engineering background in the remote broadcasting and entertainment industries.
Contact Steve via e-mail or send feedback on the VB Team Blog http://blogs.msdn.com/vbteam/
You're probably wondering, "Will my 1.1 controls work with Drag Once Databinding?" Maybe. In order for VS 2005 Drag Once DataBinding features to figure out what property to bind to, you may need to crack open the code and add one or more of the binding attributes. Early on, we talked with component vendors and they plan to quickly release updated controls with the new attributes.