Learning Resources
 

Sorting editing and deleting with the GridView


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.

The GridView control does not perform its own sorting of columns, but rather relies on the data source control to perform sorting on its behalf. The control provides the user interface (UI) for sorting, such as LinkButton controls displayed at the top of each column of the grid. However, the GridView control relies on the data-sorting capabilities of the data source control to which it is bound.

If the bound data source control can sort data, then the GridView control can interact with the data source control and request sorted data by passing a SortExpression to the data source when data is selected. Not all data source controls support sorting; for example, the XmlDataSource control does not. If the data source control supports sorting, however, the GridView can take advantage of it. The following list describes data source controls and the configuration that is needed to support sorting:

GridView Sorting Process

You can enable the default sorting behavior in the GridView control by setting its AllowSorting property to true. Setting this property to true causes the GridView control to render a LinkButton control in the column headers. The control also implicitly sets the SortExpression property of each column to the name of the data field to which it is bound. For example, if the grid contains a column that displays the City column of the Employees table in the Northwind sample database, the SortExpression property of that column will be set to City.

At run time, users can click the LinkButton control in a column heading to sort by that column. Clicking the link causes the page to perform a postback and raises the GridView control's Sorting event. The sort expression — by default, the name of the data column — is passed as part of the event arguments. The default behavior for the Sorting event is that the GridView control passes the sort expression to the data source control. The data source control executes its selection query or method, including the sort parameters passed by the grid.

After the query has executed, the grid's Sorted event is raised. This event allows you to perform post-query logic, such as displaying a status message. Finally, the data source control rebinds the GridView control to the results of the resorted query.

The GridView control does not check whether the data source control supports sorting; it always passes the sort expression to the data source. If the data source control does not support sorting and a sorting operation is performed in the GridView control, the GridView control throws the NotSupportedException exception. You can catch this exception in a handler for the Sorting event and check the data source to determine whether sorting is supported, or by using your own sorting logic.

Setting the AllowSorting property of the control enables sorting for columns by default. You can disable sorting for individual fields (for example, the BoundField or TemplateField field) by setting the SortExpression property of the individual column to an empty string ("").

You can add styles to the column that the data is sorted by, and you can display arrows in the column heading to indicate whether sorting is ascending or descending. You can also change the style of the column that the data is sorted by based on the sort direction. The TableItemStyle properties enable you to add style settings to the GridView columns, as listed in the following table.

 

Property

Description

SortedAscendingHeaderStyle

When this style is set, an arrow that points up is displayed in the column heading when the data in that column is sorted in ascending order.

SortedAscendingCellStyle

When this style is set, the style is applied to cells when the data in that column is sorted in ascending order.

SortedDescendingHeaderStyle

When this style is set, an arrow that points down is displayed in the column heading when the data in that column is sorted in descending order.

SortedDescendingCellStyle

When this style is set, the style is applied to cells when the data in that column is sorted in descending order.

If the default sort behavior is not adequate for your requirements, you can customize the grid's sorting behavior. The basic technique for custom sorting is to handle the Sorting event. In the handler, you can do the following:

  • Customize the sort expression that is passed to the data source control. By default, the sort expression is the name of a single column. You can modify the sort expression in the event handler. For example, if you want to sort by two columns, you can create a sort expression that includes both. You can then pass the modified sort expression to the data source control. 

  • Create your own sorting logic. For example, if you are working with a data source that does not support sorting, you can perform the sort in your own code and then bind the grid to the sorted data.

 

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.


The GridView control supports paging over the items in its data source. You set the AllowPaging property to true to enable paging. The GridView control supports paging in one of these ways:

  1. If the GridView control is bound to a data source control that is capable of returning a single page of data when requested, the GridView control will take advantage of that capability directly. The number of rows requested may vary depending on the number of rows per page specified by the PageSize property, and whether the data source supports getting the total row count.

    Note Note

    Of the data source controls that are included in the .NET Framework, only the ObjectDataSource control supports returning a single page of data.

    Note Note

    If you are creating a data source (such as implementing a SelectCountMethod method in the source object for the ObjectDataSource control), it is strongly recommended that your data source return the total row count when supplying pages of data. This minimizes the number of records that the GridView control must request in order to retrieve a page of data. If the total row count is supplied by the source data object, the GridView control will request only a page of rows at a time. If the total row count is not supplied, the GridView control must request all rows from the data source (starting with the row that represents the requested page of data) and discard all rows except the row being displayed.

  2. If the GridView control is bound to a data source control that does not support the paging capability directly, or if the GridView control is bound to a data structure in code through the DataSource property, the GridView control will perform paging by getting all of the data records from the source, displaying only the records for the current page, and discarding the rest. This is supported only when the data source for the GridView control returns a collection that implements the ICollection interface (including datasets).

    Note Note

    If the data source does not support paging directly and does not implement the ICollection interface, the GridView control cannot page. For example, if you are using a SqlDataSource control and have set its DataSourceMode property to DataReader, the GridView control cannot implement paging.

You can customize the paging user interface of the GridView control in a number of ways. You can set the size of the page (that is, the number of items to display at once) using the PageSize property. You can also set the current page of the GridView control by setting the PageIndex property. You can specify more custom behavior using either the PagerSettings property or by supplying a pager template.

Paging Modes

The PagerSettings property allows you to customize the appearance of the paging user interface (UI) that is automatically generated by the GridView control when you set the AllowPaging property to true. The GridView control can display direction controls that allow forward and backward navigation as well as numeric controls that allow a user to move to a specific page.

The PagerSettings property of the GridView control is set to a PagerSettings class. You can customize the paging mode by setting the Mode property of the GridView control. For example, you can customize the paging UI mode by setting it as follows:

 
 
GridView1.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast

The available modes are:

Pager Control Appearance

The GridView control has numerous properties that you can use to customize the text and images for different pager modes. For example, if you want to allow navigation using direction buttons but want to customize the text that appeared, you can customize the button text by setting the NextPageText and PreviousPageText properties, as in the following example:

 
 
GridView1.PagerSettings.NextPageText = "Click for next page"
GridView1.PagerSettings.PreviousPageText = "Click for previous page"

You can also use images to customize the appearance of your paging controls. The PagerSettings class includes image URL properties for the first, last, previous, and next page command buttons.

Finally, you can control the appearance of the paging commands by setting the PagerStyle property of the GridView control to a TableItemStyle value.

Persisting Row Selection During Paging

You can specify how row selection is persisted when the GridView control is in paging mode. By default, row selection is persisted by row index. For example, if the second row in a page is selected, the second row in all pages will be selected.

Alternatively, you can specify that row selection is persisted by data-row key. In that case, when a row is selected, rows in other pages are not selected. To enable this functionality, set the EnablePersistedSelection property to true as shown in the following example:

 
 


NoteNote

In versions of ASP.NET earlier than version 4, row selection could only be based on row index. Row selection is based on index by default for backward compatibility.

Data Paging Template

If you set the AllowPaging property of the GridView control to true, the GridView control automatically adds user interface (UI) controls for paging. You can customize the UI for paging by adding a PagerTemplate template. To specify which paging operation to perform, include a Button control with its CommandName property set to Page and a CommandArgument set to one of the following values:

  • First    To move to the first page.

  • Last    To move to the last page.

  • Prev    To move to the previous page.

  • Next    To move to the next page of data

  • A number   To move to a specific page.

The GridView control raises two events when it moves to a new page of data. The PageIndexChanging event occurs before the GridView control performs the paging operation. The PageIndexChanged event occurs after the new page of data has been returned to the GridView control.

You can use the PageIndexChanging event to cancel the paging operation, if needed, or to perform a task before the GridView control requests a new page of data. You can use the PageIndexChanged event to perform a task after the user moves to a different page of data.

--MSDN