2.2. Generating a Quick-and-Dirty Tabular Display


You want to display data from a database in a table, and you’re not overly concerned about performance or your ability to control the arrangement of the data items within the display.


Use a GridView control and bind the data to it.

In the .aspx file, add the GridView control responsible for displaying the data.

In the code-behind class for the page, use the .NET language of your choice to:

  1. Create a SqlDataSource.

  2. Set the ConnectionString, DataSourceMode, ProviderName, and SelectCommand properties of the SqlDataSource.

  3. Assign the data source to the GridView control and bind it.

Figure 2-1 shows the appearance of a typical GridView in a browser. Examples 2-1 through 2-3 show the .aspx and VB and C# code-behind files for the application that produces this result.

Quick-and-dirty GridView output

Figure 2-1. Quick-and-dirty GridView output


Implementing a simple GridView requires little coding. You must first add a GridView control to the .aspx file for your application and set a few of its attributes, as shown in Example 2-1. The GridView control has many attributes you can use to control the creation of a GridView object, but only three are required for this example: the id, runat, and AutoGenerateColumns attributes. The id and runat attributes are required by all server controls. When the AutoGenerateColumns attribute is set to True, it causes the ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.