Many Records at a Time: GridView

Whereas the DetailsView control works with one record from a query at a time on-screen, the GridView control displays many, as shown in Figure 8-14. It is the go-to control for displaying tabular data on-screen and, as it is also derived from the CompositeDataBoundControl class, it shares many of its properties, events, and methods with the DetailsView control.

To demonstrate, create a new web page called GridView.aspx in the C8_DataAccess website and drag a SqlDataSource and a GridView control onto it. Set the DataSource to select all the data from the Customer table and then set the GridView to use the data source using its DataSourceID property, as shown in Example 8-6.

Example 8-6. Bare-bones markup for GridView.aspx

<%@ Page Language="C#" AutoEventWireup="true"
   CodeFile="GridView.aspx.cs" Inherits="GridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>GridView Demo</title>
</head>

<body>
   <form id="form1" runat="server">
   <div>
      <asp:SqlDataSource ID="dsCustomers" runat="server"
         ConnectionString="<%$ ConnectionStrings:AWLTConnection %>"
         SelectCommand="SELECT * FROM [SalesLT].[Customer]" />
      <asp:GridView ID="gvwCustomers"
         runat="server" DataSourceID="dsCustomers" />
   </div>
   </form>
</body>
</html>

If you compare this to the bare-bones markup for the DetailsView in Example 8-3, you’ll see the code is ...

Get Programming ASP.NET 3.5, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.