Exforsys.com
 
Home Tutorials ASP.NET 2.0
 

ASP.NET 2.0 : Adding Sorting and Paging in GridView

 

ASP.NET 2.0 - Event handler for Sorting Event

Page 2 of 3


To add an event handler for the Sorting event

1. If you are working with a single-file page, switch to Source view and then add the following directives to the top of the code page:



< % @ Import Namespace="System.Data" % >
< % @ Import Namespace="System.Data.SqlClient" % >



2. Importing the namespaces will make it easier to write the code needed.


3. If the application is using a code-behind page, switch to the code-behind page Default.aspx.cs and add the following lines to the top of the code file, outside the class declaration:


using System.Data;
using System.Data.SqlClient;



4. In the Default.aspx page, switch to Design view, right-click the DropDownList control, and then click Properties.



5. In the Properties window toolbar, click the Events icon, and then double-click SelectedIndexChanged to create a skeleton event handler.


6. Insert the following highlighted code into the event handler.


Click here to view sample code


/span>


This code intercepts the SelectedIndexChanged event for DropDownList1 to specify the second sort parameter based on the value selected. Then, it retrieves the sortorder from the Session state and adds the " ASC" or " DESC" to the Sort property for the DataView. Finally, it rebinds the GridView control to the data.


7. Switch to the Default.aspx page and select the DropDownList control.


8. In the Properties window toolbar, click the Events icon, and then double-click Load to create a skeleton event handler.



9. Insert the following highlighted code into the Page_ Load event handler.


Click here to view sample code


10. This code handles the Page Load event and retrieves the ConnectionString connection string from the Web.config file to execute a SELECT statement using a sqlDataAdapter control. Then, it fills a dataset with the data returned, selects the first table of the dataset as a DataView object, for sort operations, and then saves the DataView object in the Session state such that subsequent sorts do not require a round trip to the database. Finally, it sets the default sort order to Ascending in Session state.


11. Switch to Design view.


12. Right-click the GridView1 control, click Properties, and then set DataSourceID to (None) to remove it.


13. Because you are setting the data source in code, the GridView control cannot also have a DataSourceID property setting.


14. When prompted to refresh the fields and keys for the GridView control, click No.


15. Although you are changing the data source for the GridView control (by removing it), you want to retain the existing field bindings.


16. Right-click the GridView1 control, and then click Properties.


17. In Properties, click Events, and then double-click Sorting to create a skeleton event handler.


18. Add the following highlighted code to the Sorting event handler:


Click here to view sample code



This code handles the sorting event of the GridView control to customize the sort functionality of the GridView control. It uses the sorting functionality of the underlying DataView control. It retrieves the DataView control saved in the Session state. Then, it retrieves the sort order value from Session state and adds " ASC" or " DESC" to theSortExpression property passed to the event handler. It saves the new sort order and the SortExpression value in the Session state. Finally, it displays the SortExpression value in a Label control and rebinds the GridView control to the data. The page can now be tested.




Next Page: ASP.NET 2.0 - GridView Control Data





 

 

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

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 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape