Comparing the GridView and DataGrid Web Server Controls

The GridView control is the successor to the DataGrid control. Like the DataGrid control, the GridView control was designed to display data in an HTML table. When bound to a data source, the DataGrid and GridView controls each display a row from a DataSource as a row in an output table.

Both the DataGrid and GridView controls are derived from the WebControl class. Although it has a similar object model to that of the DataGrid control, the GridView control also has a number of new features and advantages over the DataGrid control, which include:

  • Richer design-time capabilities.

  • Improved data source binding capabilities.

  • Automatic handling of sorting, paging, updates, and deletes.

  • Additional column types and design-time column operations.

  • A Customized pager user interface (UI) with the PagerTemplate property.

Differences between the GridView control and the DataGrid control include:

  • Different custom-paging support.

  • Different event models.

Improved Design-Time Capabilities

Sorting, paging, and in-place editing of data requires additional coding when using the DataGrid control. The GridView control enables you to add sorting, paging, and editing capabilities without writing any code. Instead, you can automate these tasks, along with other common tasks such as data binding to a data source, by setting properties on the control.

If you are working in a designer, such as Microsoft Visual Studio, you can take advantage of designer features built into the GridView control. The GridView control includes support for a smart tag panel that provides a convenient interface for performing common tasks, such as setting properties and launching template editing.

Improved Data Source Binding Capabilities

Typically a DataSet control, a DbDataReader control, or a collection, such as an Array, an ArrayList, or some other class in the System.Collections namespace, is assigned to the DataSource property of either the DataGrid control or the GridView control. The DataGrid control and the GridView control can bind any object that implements the IEnumerable or IListSource interface.

While the DataGrid control can declaratively bind a DataSourceControl control, it can do so only for data selection. Sorting, paging, updates, and deletions must be coded manually. The GridView control supports a DataSourceID property that takes any object that implements the IDataSource interface and can, therefore, take advantage of the data source control's sorting, paging, updating, and deleting capabilities, for example, the SqlDataSource control.

Additional Column Types

The GridView control supports the following column types: BoundField, HyperLinkField, ButtonField, CommandField ImageField, and CheckBoxField.

Built-In and Custom Paging Support

The DataGrid control requires additional coding for paging. The GridView control automatically supports paging by setting the PagerSettings property. The PagerSettings property supports four modes: Numeric (default), NextPrevious, NumericFirstLast, and NextPreviousFirstLast. The Numeric mode displays numbered page links instead of "next/prev" links, and the NumericFirstLast option adds first and last page links. The GridView control's PagerStyle property can be used to set styles and the position of the pager.

Additionally, you can customize the pager buttons for the GridView control using a PagerTemplate.

Custom paging support in the GridView is supplied by the bound data source control as opposed to the AllowCustomPaging mechanism supplied by the DataGrid control.

Expanded Event Model

The DataGrid and the GridView controls have different event models.

The DataGrid control raises single events for operations, while the GridView control is capable of both pre-operation and post-operation events. The GridView control supports the Sorting event that occurs when a field is sorted. Note that this sorting event occurs before the GridView control automatically handles the sort operation, giving you the opportunity to examine or change the SortExpression property, or cancel this operation by setting the Cancel property to true on the passed event arguments.

The GridView control supports the Sorted event that occurs after the GridView control completes the sort operation, giving you the opportunity to change or format the result of the sort operation. In comparison, the DataGrid control supports the SortCommand event that occurs when a column is sorted. Similarly, the GridView control supports RowUpdating and RowUpdated events that occur before and after the GridView control has automatically handled the update operation. In comparison, the DataGrid control supports the UpdateCommand event that occurs when the Update button is clicked for an item in the grid.

For more information on GridView events, see GridView Web Server Control Events.

See Also


Paging in a GridView Web Server Control

Modifying Data in a GridView Web Server Control

Walkthrough: Simple Sorting for the GridView Web Server Control