JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Binding Data Grids

The quintessential data-bound control is the data grid, so let's see an example using a Web server data grid. This example will be called WebDataGrids on the CD-ROM. To create this example, just drag an OleDbAdapter object onto a Web form, and use the Data Adapter Configuration Wizard to make the adapter return the authors table from the pubs database. Next, create a dataset from that adapter, using the Data|Generate Dataset menu item. Finally, drag a data grid onto the Web form, and connect its DataSource property to the dataset you've created, and the DataMember property to the authors table in the dataset.

You'll also need to add some code in the Page_Load event handler (alternatively, you can add a Load button to the form) to fill the dataset with data from the adapter, and to bind the data grid to that data. Here's what that looks like:

Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    DataSet11.Clear()
    OleDbDataAdapter1.Fill(DataSet11)
    DataGrid1.DataBind()
End Sub

That's all it takes—you can see the results in Figure 23.2, where the data grid is displaying the authors table.

Click To expand
Figure 23.2: Displaying data in a data grid in a Web application.

The data grid you see in Figure 23.2 looks pretty plain, and it's obviously modeled on the HTML <table> element. You can customize a data grid easily—just right-click the data grid and select the Auto Format item, opening the Auto Format dialog you see in Figure 23.3. This dialog lets you select from a number of pre-built styles for the data grid, setting header color, border width, and so on.

Click To expand
Figure 23.3: Customizing a data grid in a Web application.

You also can customize the data grid with the Property Builder tool—just right-click the data grid and select the Property Builder item, opening that tool as you see in Figure 23.4. For example, in the Property Builder, you can select which columns the data grid should display (for an example of this, see "Creating Master/Detail Web Forms" in the Immediate Solutions section of this chapter), what borders to use, and whether or not to use paging. Paging lets you display a table in pages, where only a few records are visible in a data grid one page at a time. The user clicks hyperlinks to see additional pages in which the data grid displays additional records.

Click To expand
Figure 23.4: Using the Property Builder.

As always, data grids are easy to use and are useful for displaying an entire table. But what if we want to bind list boxes or text boxes? I'll take a look at doing that next.

Tip 

In fact, you can make a column of data in a data grid display hyperlinks, and the user can click those hyperlinks to make a SelectedIndexChanged event occur. See "Creating Master/Detail Web Forms" in this chapter for the details.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor