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.
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:
- The SqlDataSource and AccessDataSource controls can sort if the DataSourceMode property is set to DataSet, or the SortParameterName property is set to either DataSet or DataReader.
- The ObjectDataSource control can sort if its SortParameterName property is set to a value supported by the underlying object.
GridView Sorting Process
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.
|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.|
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:
- 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 Of the data source controls that are included in the .NET Framework, only the ObjectDataSource control supports returning a single page of data. 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.
- 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).
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.
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:
<asp:GridView id="GridView2" runat="server" PersistedSelection="true"> </asp:GridView>
|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.