|
Page 1 of 3
.ASP.NET 2.0 Free Training : Adding Sorting and Paging in GridView
In this tutorial you will learn adding sorting and paging in a GridView Control, Implement Two Column Sorting, Create Code for Custom Sorting, Editing the GridView control data, Deleting Displayed Records, Inserting Records and Using Templates.
Adding Sorting and Paging
Sorting and paging can be added to the GridView control without writing any code.
1. In Design view, right-click the GridView control, and then click Show Smart Tag.
2. On the GridView Tasks menu, select the Enable Sorting box.
3. The column headings in the GridView control change to links.
4. On the GridView Tasks menu, select the Enable Paging box.
5. A footer is added to the GridView control with page number links.
6. Optionally, use the Properties window to change the value of the PageSize property from 10 to a smaller page size.
7. Press CTRL+F5 to run the page.
8. The GridView control is displayed with StudentID, StudentName, StudentAge and CourseId columns. The column headings can be clicked to sort by the contents of that column.

9. Close the browser.
Implementing Two Column Sorting
At this point, the contents of the table can be sorted on the basis of a single column. However, there may be situations where the developer needs to provide an interface to sort data on the basis of two or more columns. To implement two-column sorting, custom sorting features may be added. In this section, let us add a DropDownList control to specify a second column to sort the grid. The DropDownList control will display the names of columns from the StudentMaster table that users can sort by.
1. In the Default.aspx page, switch to Design view.
2. In the Toolbox, from the Standard group, drag a DropDownList control onto the page.
3. Right-click the DropDownList control, click Show Smart Tag, and then on the Common DropDownList Tasks menu, click Edit Items.

4. In the ListItem Collection Editor dialog box, click Add, and set Text and Value to StudentName.
5. Click Add to add another list item, and then set Text and Value to CourseId.
6. Click Add, and then set Text and Value to StudentAge.
7. Click Add, and then set Text and Value to StudentId.

8. Set Selected to True.
9. Click OK.
10. On the Common DropDownList Tasks menu, select the Enable AutoPostBack box.

11. In the Toolbox, from the Standard group, drag a Label control onto the page.
12. Right-click the Label control, click Properties, and then set ID to LabelSortDisplay.
Creating Code for Custom Sorting
For custom sorting, we now write an event handler for the GridView control's Sorting event. The Sorting event is raised when the user clicks a column heading in the GridView control.
|