Binding Data to Multi-Value Web Controls

Binding data to a DataGrid , DataList , or Repeater control is easiest of all. All you have to do is have an object that implements IEnumerable , assign this object to the DataSource property of the prospective control, and invoke DataBind on that control. The ease with which you can get data into these controls in no way mitigates their utility. You can perform mundane tasks , like simply binding a DataView object, or create elaborate, complex interfaces with a bit of clever invention. (Read the Using a DataList Control to Repeat Composite Controls section later in this chapter for more information.)

Refer to Listing 16.4 for a quick example of getting a DataView object into a DataGrid control. Listing 16.7 demonstrates how we can use a DataGrid control and specific bound columns as an alternate way to refine the view of data. Listing 16.7 uses the lazy Contacts instance from Listing 16.6 but specifies bound columns instead of auto-generating columns. In the example I bound only the contact name and phone number from the Contacts data view. Figure 16.8 shows the results.

Listing 16.7 Binding Specific Data to a DataGrid Control
 Private Sub Page_Load(ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles MyBase.Load   ' Binds the Contacts data view to the DataList control   DataGrid1.DataSource = Contacts   DataGrid1.DataBind() End Sub 
Figure 16.8. The result of binding specific columns to a DataGrid control instead of allowing the grid to autogenerate the columns.

graphics/16fig08.jpg

The code in Listing 16.7 relies on the previously mentioned lazy instance of the Contacts data view from the Contacts property, the GetContacts method, and the Database class. It is important to understand that the code in Listing 16.7 demonstrates how we would bind any data source to a DataGrid control.

Here are the steps for limiting the bound columns for the DataGrid control, as shown in Figure 16.8.

  1. Select the DataGrid control.

  2. Open the Properties window and change the DataGrid control's AutoGenerate Columns property to False .

  3. Right-click the DataGrid control and select Property Builder from the context menu or select Property Builder from the Properties window when the DataGrid control is selected.

  4. Select Bound Column from the Available columns list (Figure 16.9) and add two Bound Columns to the Selected columns list.

    Figure 16.9. Defining bound columns in the DataGrid control's Properties dialog.

    graphics/16fig09.gif

  5. Select the first Bound Column and change the Header text to Contact and the Data Field to ContactName. Figure 16.9 shows these settings.

  6. Select the second Bound Column and set the Header text to Phone and the Data Field to Phone.

  7. Click OK to save your changes.

If you add the code from Listing 16.7 to the Page_Load event in Listing 16.6, you can run the application and see the results as shown in Figure 16.8.

Using the DataGrid control in the manner prescribed is pretty basic. In the next section we'll look at how the DataGrid control supports paging and sorting. The section after that will demonstrate how we can use a variation of the technique described in this section to repeat complex user controls, resulting in professional and elaborate-looking user interfaces.



Visual Basic. NET Power Coding
Visual Basic(R) .NET Power Coding
ISBN: 0672324075
EAN: 2147483647
Year: 2005
Pages: 215
Authors: Paul Kimmel

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