2.2. Generating a Quick-and-Dirty Tabular Display
Problem
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.
Solution
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:
Create a
SqlDataSource
.Set the
ConnectionString, DataSourceMode, ProviderName
, andSelectCommand
properties of theSqlDataSource
.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.
Figure 2-1. Quick-and-dirty GridView output
Discussion
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.