Exforsys.com
 
Home Tutorials ASP.NET 2.0
 

ASP.NET 2.0 : Adding Sorting and Paging in GridView

 

ASP.NET 2.0 : Adding Sorting and Paging in GridView

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.


Next Page: ASP.NET 2.0 - Event handler for Sorting Event


Read Next: ASP.NET DataBound Controls - Details View



 

 

Comments


WhoCares said:

  More interesting would be custom paging with custom sorting. Your sample is trivial.
March 21, 2007, 8:16 am

yogarajan said:

  Hi all,
I need to display two sets of records.
The second one should come under the first set of records and also second one uses the first one's unique id.
for example,
First table values
id Show/hide button checkbox category
1 car (parent)
Maruthi (child)
BMW(child)
HeroHonda(child)
Yamaha(child)

2 bike

3 fruits
Apple
Orange
Mango
.
.
.
(etc)


1. if i click show button that time only child grid is show . button caption change to 'hide'
2. if i click hide button that time only child grid is hide. button caption change to 'show'

2. if i select parent checkbox (ie. checked)(ex. parent -> car ) that corresponding child grid (ex. child -> Maruthi, BMW, HeroHonda, Yamaha) also select(ie. checkbox checked)
-----------
So, How to Do it?.
August 30, 2007, 12:41 am

geetha said:

  Hi,
I am having checkbox column in my gridview.When I select the checkbox in header,All checkboxes in the row also selected.But when I click the page number to view the next page,the checkboxes become unselected.This is my problem that all checkboxes should be in selection even in pagination.Answer me.
March 2, 2009, 11:34 pm

Rutika said:

  Hi,
When I am using search in datagrid then first time it shows correct search but when I click 2 in pagination then again it shows all records why?
November 6, 2009, 2:35 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape