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 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.
Using the GridView you can:
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:
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.
The GridView control is the successor to the control. For details about differences between the controls, see Comparing the GridView and DataGrid Web Server Controls.
Data Binding with the GridView Control
The GridView control provides you with two options for binding to data:
Data binding using the 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 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 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 ASP.NET Data-Bound Web Server Controls Overview.
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 or 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 class.
GridView Sorting Functionality
The GridView control supports sorting by a single column without requiring any programming. You can further customize the sort functionality of the GridView control by using the sort event and providing a sort expression. For more information, see Sorting Data in a GridView Web Server Control.
GridView Paging Functionality
The GridView control provides a simple paging functionality. You can customize the paging functionality of the GridView control by using the property of the GridView control. For more information, see Paging in a GridView Web Server Control.
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. For more information, see GridView Web Server Control Events.