GridView Web Server Control Overview

A recurring task in software development is to display tabular data. ASP.NET provides a number of tools for showing tabular data in a grid, including the GridView control. With the GridView control, you can display, edit, and delete data from many different kinds of data sources, including databases, XML files, and business objects that expose data.

This topic contains:

  • Background

  • Code Examples

  • Class Reference

Background

You can use the GridView control to do the following:

  • Automatically bind to and display data from a data source control.

  • Select, sort, page through, edit, and delete data from a data source control.

Additionally, you can customize the appearance and behavior of the GridView control by doing the following:

  • Specifying custom columns and styles.

  • Utilizing templates to create custom user interface (UI) elements.

  • Adding your own code to the functionality of the GridView control by handling events.

Data Binding with the GridView Control

The GridView control provides you with two options for binding to data:

  • Data binding using the DataSourceID property, which allows you to bind the GridView control to a data source control. This is the recommended approach because it allows the GridView control to take advantage of the capabilities of the data source control and provide built-in functionality for sorting, paging, and updating.

  • Data binding using the DataSource property, which allows you to bind to various objects, including ADO.NET datasets and data readers. This approach requires you to write code for any additional functionality such as sorting, paging, and updating.

When you bind to a data source using the DataSourceID property, the GridView control supports two-way data binding. In addition to the control displaying returned data, you can enable the control to automatically support update and delete operations on the bound data.

For more information, see Data-Bound Web Server Controls.

Formatting Data Display in the GridView Control

You can specify the layout, color, font, and alignment of the GridView control's rows. You can specify the display of text and data contained in the rows. Additionally, you can specify whether the data rows are displayed as items, alternating items, selected items, or edit-mode items. The GridView control also allows you to specify the format of the columns. For information on formatting the GridView control, see the GridView class overview.

Editing and Deleting Data Using the GridView Control

By default, the GridView control displays data in read-only mode. However, the control also supports an edit mode in which it displays a row that contains editable controls such as TextBox or CheckBox controls. You can also configure the GridView control to display a Delete button that users can click to delete the corresponding record from the data source.

The GridView control can automatically perform editing and deleting operations with its associated data source, which allows you to enable editing behavior without writing code. Alternatively, you can control the process of editing and deleting data programmatically, such as in cases where the GridView control is bound to a read-only data source control.

You can customize the input controls that are used when a row is in edit mode using a template. For more information, see the TemplateField class.

GridView Sorting Functionality

The GridView control supports sorting by the content of a single column without requiring any programming. You can customize the sort functionality of the GridView control by handling the sort event, providing a sort expression, or by applying a CSS style to the column the data is sorted by. You use the SortedAscendingHeaderStyle, SortedDescendingCellStyle, SortedAscendingHeaderStyle and SortedDescendingHeaderStyle properties to apply a CSS style to the sorted column. For example, you can specify a style for the column that the data is sorted by and apply a header style to indicate the direction of the sort. For more information, see Sorting Data in a GridView Web Server Control.

GridView Paging Functionality

The GridView control provides simple paging functionality. You can customize the paging functionality of the GridView control by using the PagerTemplate property of the GridView control.

You can specify how selected rows are persisted when the GridView control is in paging mode. By default, row selection is based on row index. The same row (for example, the third row) is selected on each page. Alternatively, you can enable persistence based on the data key of the selected row. In that case, if you select row 3 on page 1 and you move to page 2, no row is selected on page 2. If you move back to page 1, row 3 is still selected. To enable this functionality, set the EnablePersistedSelection property to true.

Note

In versions 2.0, 3.0, and 3.5 of ASP.NET, row selection was based only on row index. By default, row selection in ASP.NET 4 is based on index for backward compatibility.

For more information, see Paging in a GridView Web Server Control.

GridView Events

You can customize the functionality of the GridView control by handling events. The GridView control provides events that occur both before and after a navigation or edit operation.

Back to top

Code Examples

Walkthrough: Calling a Stored Procedure Using the SqlDataSource Web Server Control

Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control

Walkthrough: Performing Bulk Updates to Rows Bound to a GridView Web Server Control

Walkthrough: Creating a Nested GridView Control

Creating a Custom Column in a GridView Web Server Control

How to: Set GridView Web Server Control Column Width Dynamically

Back to top

Class Reference

The following table lists the key classes that relate to the GridView control.

Member

Description

GridView

The main class for the control.

GridViewRow

Represents an individual row in a GridView control.

Back to top

See Also

Concepts

Data-Bound Web Server Controls

Sorting Data in a GridView Web Server Control

Paging in a GridView Web Server Control

Modifying Data in a GridView Web Server Control

ASP.NET Web Server Controls Templates

Other Resources

ASP.NET Data Access Content Map