Bidirectional Sorting on Any Column in DataGrid
page 6 of 7
by Pani Baruri & Abhijit Mandrekar
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25219/ 54

Part 2: Psuedo Logic
  1. Add dynamic attributes sortexpression and sortdirection to datagrid first time the page is loaded.

 

  1. Assign appropriate column name and sort order to sortexpression and sortdirection attribute respectively to sort data in order to get initial look and feel of data when the page is loaded first time.

 

  1. Get dataset and dataview objects. Set sort property of dataview object to concatenated string built using sortexpression and sortdirection dynamic attributes.

 

  1. Bind datagrid using dataview object.

 

  1. On every consecutive page loads and column clicks, trap sortcommand event of datagrid.

 

  1. Compare sortexpression property of the event argument obtained from sortcommand event with dynamic attribute sortexpression.

 

  1. If Match is found then
    1. Check sortdirection attribute.
    2. If sortdirection equals “ASC” then

                                                               i.      Assign DESC to sortdirection attribute.

    1. Else

                                                               i.      Assign ASC to sortdirection attribute.

    1. End If

 

  1. Repeat steps from Step 3 onwards.

 

Note: When you run the following code and click the same column two or more times then sort direction is reversed automatically for that column. However if column 3 is clicked followed by column 1 and 2 click then column 3 is sorted only in ascending direction no matter what the sort direction was before for the same column. This is because previous sort order is not preserved in the sortexpression property of the column. The overall result is same depending how you perceive it. This example teaches you how to make use of dynamic attributes and dataview object.


View Entire Article

User Comments

Title: Bidirectional Sorting on Any Column in DataGrid   
Name: brian
Date: 2007-08-08 10:39:14 PM
Comment:
thanks a lot.It really very helpful
thumb up

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 5:07:22 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search