Binding to the TableAdapter Using Drag and Drop


Once you "finish" with a TableAdapter configuration, you're almost ready to run the code that's been generated. As I step through the demos you've seen so many times, let's take the next step and drag a TableAdapter from the Data Sources window to a Windows Form. But waitbefore you drag anything from the Data Sources window, click the selected TableAdapter icon and click the down arrow. This exposes a menu, as shown in Figure 6.31.

Figure 6.31. Choosing how the TableAdapter is to be rendered when dragged to a Form.


By default, dragging a TableAdapter from the Data Sources window incorporates a DataGridView as the bound control. I'll start from there and re-visit how "Details" binding works a bit later.

1.

Open the Form "design" window. It can be customized as needed, but the process of dragging a TableAdapter pretty much takes over the Form, so other elements might be completely or partially overlaid.

If you don't like the placement of the controls placed on your Form, you can click Ctrl+Z to undo the drag-and-drop operation or simply rearrange the controls as needed.


2.

Open the Data Sources window and select the TableAdapter you wish to execute. Drag the icon for this TableAdapter to the Form. Wait a few seconds while the code generator does its job. The result should look something like Figure 6.32.

Figure 6.32. UI generated by dragging the Author's TableAdapter to a form.


Ah, your UI might not look like this at first. I set the Form.Text property and resized the form to expose the third parameter (AuthorWanted) and the "Fill" (FillToolStripButton) button. No, I don't think this "button" looks like a button, either. But that's a bug in the UI generator, and I'll fix that in a minute. I also moved the DataGridView control up and to the leftcloser to the top of the form.

First, take a closer look at the components that have been added to your Form:

  • A BindingNavigator: Docked at the top of the Form (I assume your Form looks something like Figure 6.32), you'll find a customized BindingNavigator control (new for Visual Studio 2005), as highlighted in Figure 6.33. This control includes several functional elements:

    • Navigation controls: The first, previous, next, and last VCR-style buttons permit you to add code to move the current row pointer. If you double-click on these buttons at design time, you won't see any codeit's there though, hidden behind the scenes in the BindingNavigator control. It's also bound to the DataTable returned by the TableAdapter.

    • Row position and total rows count: The BindingNavigator control displays the current row pointer and the DataTable.Rows.Count property.

    Figure 6.33. The customized BindingNavigator.


  • An Add, Delete, and Save button: By default, these buttons are wired to the DataAdapter Update method built by the TACW. If you didn't choose to include code generation for these operations, the event handlers for these buttons must be populated with your own custom update logic.

  • The "Fill" ToolStripPanel: The ToolStripPanel (new for Visual Studio 2005) is simply a dockable container for the Fill Button control and any query parameters you've defined. Each parameter is exposed in its own labeled TextBox control. You're going want to edit the labels to make them readable as well as supply default values for the parameters.

  • A DataGridView control: Because I choose the default rendering option, dragging the TableAdapter to the form instructs the code generator to bind to a DataGridView (new for Visual Studio 2005). The control is placed at the cursor "drop point", so you'll probably want to reposition it or dock it to the Form.

  • A BindingSource control: At the bottom of the Form, you'll find icons for all of the controls added to the Form, including a BindingSource. This control (described in more detail in Chapter 7, "Managing Data Tools and Data Binding") binds a data source with one or more bound controls.

  • A generated DataSet: The drag-and-drop code generator exposes a custom DataSet named after and returned by the TableAdapter. This is bound to the BindingSource.

  • A TableAdapter: The drag-and-drop code generator also builds a custom TableAdapter for the form. This is used to expose the DataSet as a data source.

Customizing the Generated UI

Notice that the "Fill" button on the FillToolStrip does no look much like a Buttonor, at least, I don't think it does. To fix this, you can right-click on the "Fill" button and choose "Set Image" from the menu, as shown in Figure 6.35. Choose an appropriate image file and set the DisplayStyle to "Image" or "Image and Text". This will let your users have a better chance at finding the "Fill" button. No, I don't think it's a good idea to pre-populate the Form with datanot until the user has chosen the input parameters. This makes your Form load faster. Sure, if you save the user preferences, you might return to the last set of parameters and execute Fill when the form is loaded using those parameters.

Figure 6.34. The controls added to the Tool Tray area of the Form by drag-and-drop.


Figure 6.35. Customizing the "FillToolStrip" button.


Since I'm focusing on parameters, I need to make a few more changes to the generated codeat least, indirectly. I need to set the parameter default values (as I have already done in Figure 6.34). Click on each of the TextBox controls in the FillToolStrip and set the Text property. Where do these default parameters come from? Well, I like to "remember" the parameters last used, so I save the values in a persisted application setting that's reloaded when the application is restarted.

Using SmartTags to Manage Bound Controls

Each of the bound controls placed on the Form by the code generators can be further tuned by using the SmartTag exposed in the upper-right corner of the control. To see what's available for each bound control, I suggest you visit each control and click on the SmartTag and review what options are exposed. I clicked on the SmartTag exposed on the DataGridView to expose the "tasks" menu, as shown in Figure 6.36.

Figure 6.36. Clicking the SmartTag exposes additional "task" settings.


Note that these "tasks" permit you to easily enable DML changes, column reordering, and docking options. I talked about using this approach to make UI (instead of query) changes earlier in this chapter. You can also choose to edit or add columns to the DataGridView using this interface. One change you might want to make is to make sure any RO columns in the DataGridView Columns collection have their ReadOnly property set to True.

1.

Click on the DataGridView SmartTag and click on Edit Columns... to expose the Bound Columns Properties page, shown in Figure 6.37.

Figure 6.37. Editing DataGridView Column properties via the SmartTag tasks interface.


2.

Next, choose the column to edit and make whatever changes you feel are necessary.

Binding to "Details"

Just before I dragged the TableAdapter to the Form, I made a decision to use the default binding controlthe DataGridView. An alternative would be to choose "Details" from the menu shown in Figure 6.31. Let's see what happens when you make this choice.

1.

Return to the Data Sources window and click the Authors TableAdapter.

2.

Choose "Details" from the drop-down menu and drag the icon to an empty Form. Can you drag more than one TableAdapter to a form? Sure. You won't want to, thoughthe generated UI is not particularly pretty.

Figure 6.38. Choosing "Details" binds the TableAdapter data to individual controls.


Binding to Custom Controls

Sure, you don't have to use a TextBox control to expose the contents of a TableAdapter column. You can navigate to the Data Source and click on each individual column returned by the query and choose the type of control to be used (as shown in Figure 6.39). By default, the code generators try to map appropriate bound controls to each column. This way, string and number columns are bound to TextBox controls, but pictures (TEXT, IMAGE, or VarChar(Max)) data types are mapped to Image controls. You can also code and specify custom bound controls. For columns you don't want users to change, you can set the bound control type to Labelat least, this works when binding using the "details" mode. Changing the type to "Label" has no impact on the RO status of the DataGridView column.

Figure 6.39. Clicking on individual columns determines how the column is to be bound.





Hitchhiker's Guide to Visual Studio and SQL Server(c) Best Practice Architectures and Examples
Hitchhikers Guide to Visual Studio and SQL Server: Best Practice Architectures and Examples, 7th Edition (Microsoft Windows Server System Series)
ISBN: 0321243625
EAN: 2147483647
Year: 2006
Pages: 227

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net