Multiple Column Sorting and Paging in a DataGrid
page 3 of 5
by souri challa
Feedback
Average Rating: 
Views (Total / Last 10 Days): 54648/ 82

Sorting the DataGrid

 [ Download Code ]

The code for the SortCommand event handler is shown below. In a nutshell, the event handler determines a combined sort expression, sets up a DataView on the current data source with that sort expression, and binds the DataGrid to the DataView.

/// <summary>
///  Datagrid Sort Event Handler -> Determine a combined sort expression and apply.
/// </summary>
private void dgEmployee_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
  DisplaySortedData(GetSortExpression(dgEmployee,e));
}
/// <summary>
///  Apply the Sort Expression to the retrieved data and bind to datagrid
/// </summary>
private void DisplaySortedData(string sortExpression)
      {
            sortExpression = sortExpression.TrimEnd(",".ToCharArray());
            DataSet dsEmployees = new DataSet();
            dsProjects.ReadXml(Server.MapPath("Employee.xml"));
                  
            DataView dvEmployee = new DataView(dsEmployees.Tables[0]);
            dvEmployee.Sort =  sortExpression;
            dgEmployee.DataSource = dvEmployee;
            dgEmployee.DataBind();
      }

Listing 3

The key to implementing the multi-column sorting is to remember the previously clicked sort columns, and to establish the new sort criteria whenever a sort column header is clicked.

In this example the sort expression is persisted between requests in a custom DataGrid attribute named SortExpression. When the page is rendered to the browser this attribute becomes the attribute of the TABLE HTML element and is simply ignored by the browsers.

Another way of persisting the sort expression would be to keep it in a ViewState variable. This method is recommended if the DataGrid itself is not kept in ViewState due to large amounts of data.


View Entire Article

User Comments

Title: sddf   
Name: dfs
Date: 2012-10-18 7:05:55 AM
Comment:
dfsdfsdf
Title: ok   
Name: ok
Date: 2012-07-12 9:00:11 AM
Comment:
hhi
Title: nfl jerseys cheap   
Name: NIKE NFL jerseys
Date: 2012-07-02 10:11:50 AM
Comment:
http://www.jersey2shop.com
http://www.cheapjersey2store.com
http://www.jerseycaptain.com
http://www.yourjerseyhome.com
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes
Cheap NFL,NBA,MLB,NHL
,heap jerseys,2012 nike nfl Jerseys,nba jersey and shorts,oklahoma city thunder jersey,official jeremy lin new york knicks jersey,NFL Jerseys Wholesale,blake griffin jersey blue,NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
,Wholesale cheap jerseys,Cheap mlb jerseys,]Nike NFL Jerseys,Cheap China Wholesae,Wholesale jerseys From China,2012 nike nfl Jerseys,Jerseys From China,,2012 nike nfl Jerseys,Revolution 30 nba jerseys,jersey of nba chicago bulls direk rose ,nfl jerseys,green bay packers jerseys wholesale,Buffalo Bills nike nfl jerseys sale,good supplier soccer jerseys,cool base mlb jerseys,Revolution 30 nba jerseys,2012 stanley cup nhl jersey,
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes. www.yourjerseyhome.com
Title: Date Sorting   
Name: saka
Date: 2008-09-29 8:11:22 AM
Comment:
When I sort a list by date. But it not sort date wise
It sort string wise.

How to sort it in datewise?

dtGridSort = policyFeeCollection.GetDataTable
dataviewGrid = dtGridSort.DefaultView
dataviewGrid.Sort = "EnteredDate Asc"
grdPolicyFee.DataSource = dataviewGrid
grdPolicyFee.DataBind()
Title: Multiple Column Sorting and Paging in a DataGrid   
Name: Lakshitha
Date: 2008-09-02 12:09:20 AM
Comment:
please let me know how do above "Multiple Column Sorting and Paging in a DataGrid" using ASP.NET 2005..... becoze asp.net2005 not there datagrid so how can do this using asp.net 2005??
Title: Excellent   
Name: Murugan
Date: 2008-05-20 12:07:40 PM
Comment:
The code was very useful.
Question: I'm writing a custom gridview and the grid does not keep the last sorting order for the next page.
Did you ever think about this? any ideas?
Thanks, Murugan
Title: hello   
Name: salman
Date: 2007-08-06 2:30:57 AM
Comment:
very good
Title: Gr8 !!!!!   
Name: Bipul
Date: 2007-05-31 6:58:17 AM
Comment:
Too good.. My requirement was not exactly like that but after some modification I have fullfilled that..

Really excellent article.....

Thanks,
Bipul
Title: Multi-Sorting code   
Name: Pri
Date: 2007-04-09 3:33:46 PM
Comment:
The code was real useful and saved lot of time. I just have one question. When I try to sort on the first column, it actually sorts on first and second column together. It look sas if both the columns are bound together. Can you please tell me how to fix this issue. Thanks again for the code.
Title: Than You   
Name: Santosh
Date: 2007-02-21 8:18:45 AM
Comment:
Hi,
This is Santosh,Nice Article & it is useful 2 me.. Thank U..
Title: Excellent   
Name: Nishant
Date: 2007-02-06 10:54:27 PM
Comment:
I appreciate your way of explaining sorting,paging in a very simple manner. It is very useful
Title: It is a Excellent articl   
Name: M.Murugesan
Date: 2006-08-21 10:04:04 AM
Comment:
it is a very excellent article,thanks a lot
Title: Great Job   
Name: Daniel
Date: 2006-07-27 5:25:51 PM
Comment:
Thank you very much. This is just what I needed. Saved me hours of work. Thank you !!!

drazumov@s3ts.com
Title: it is good   
Name: Preeti Singh
Date: 2006-06-27 6:05:23 AM
Comment:
Nice article on this topic and very useful. Thanx!

Preeti
talk_2priti@rediffmail.com
Title: Excelant   
Name: V.Rajamanikandan
Date: 2006-06-20 2:52:37 AM
Comment:
That is nice.......
Title: QUESTION   
Name: VS
Date: 2006-06-03 8:10:11 AM
Comment:
Nice work! By the way how much would this code change if we set EnableViewState="False"? Any ideas?

Thank you.
Title: Mr   
Name: James K
Date: 2006-05-23 12:16:31 PM
Comment:
Nice job
Title: TOO GOOD!   
Name: Neo
Date: 2006-05-21 9:34:58 PM
Comment:
Hey,

Amamzing piece of code,,

Keep it up.

Ta!
Title: web developer   
Name: lakshitha
Date: 2006-03-31 6:21:58 AM
Comment:
Dear Souri

Thanks for your favorable reopened for my e-mail. Your code work properly without error so I want to change code format C# to vb.
I convert it now run now every thing is ok thanks lot of you

Thanks
take care
Lakshitha
lakshithap@dsisamson.com
Title: Date Sorting for different format   
Name: Mohith
Date: 2005-10-26 9:37:17 AM
Comment:
What if the Date displayed is in following format in the grid : Sep 21, 2005 (mon/dd/yyyy) which is 107 date format code in TSQL.
Title: Well done   
Name: Salam Y. ELIAS
Date: 2005-10-14 8:45:21 AM
Comment:
Hi Souri,
Lovely and usefull article, I was justy looking to something like this, it did the job as requested.
Mayebe it would be very interesting to make something general whihi can be plugged to any DG
I have a question about the Numer one "1", you display besides the header when it is clicked. Why do you display this number?

It is always beneficial to the community to have such articles
Title: GREATE   
Name: Jayraj
Date: 2005-08-17 9:24:21 AM
Comment:
Hi,
Thanks for you help. Code developed by you is very useful. It helps me a lot so thank you once again
Title: how do this in vc6 or vc7   
Name: goli_programmer
Date: 2005-08-14 7:14:39 AM
Comment:
hi,
I programming in vc7, and I use MSDATGRD.OCX ( ditributed with visual stodio 6 ), can I add an image to it header?
if non, is there other control that can I use insted of MSDATGRD.OCX and do this?

thanks
Title: Good,   
Name: yuvageetha
Date: 2005-08-01 4:52:55 AM
Comment:
good, it would have been better if u had provided some simple method to do it...
Title: Very Good   
Name: lokesh
Date: 2005-07-28 4:02:58 AM
Comment:
Hi Challa,

This is Good.Can I expect somemore to newusers.
Title: Very good   
Name: Joseph
Date: 2005-06-28 2:08:54 AM
Comment:
A useful help
Title: Ok . Ok   
Name: Ali
Date: 2005-06-10 5:50:53 PM
Comment:
Very Gooooooooooooooooood !?
Title: Limited but interesting   
Name: Arun
Date: 2005-06-10 10:11:35 AM
Comment:
You may want to rethink your abstract since this is far from a "compreensive solution" (which just sounds cool).
But it is a nice start anyway. Thanks.
Title: Very Good   
Name: Mr Sachin Pisal
Date: 2005-06-10 6:11:32 AM
Comment:
G8 Work
Really nice one..and very helpful
Title: Can't wait to apply it to some my applications   
Name: Geoff T
Date: 2005-06-10 3:15:41 AM
Comment:
Thanks for the most generous sharing.
I hope one day I can repay this sort of thing.
Title: Well done   
Name: Ferrol
Date: 2005-06-10 2:43:09 AM
Comment:
Hi Souri,

Nice article on this topic and very useful. Thanx!
Title: Excelant   
Name: Harshad
Date: 2005-06-09 9:47:38 PM
Comment:
It's a very very good article.
Title: very good   
Name: ali
Date: 2005-06-09 5:10:54 PM
Comment:
nice, useful piece of code
Title: ok it is very good   
Name: Lang
Date: 2005-05-31 2:20:38 AM
Comment:
ok code good thank you posted for everybody
Title: Fentastic   
Name: Girish
Date: 2005-05-23 4:55:17 AM
Comment:
Hi Souri, Very Good article, and it has solved my requirement to maximum level. keep it up and share such things to the devlopers comunity.

Regards,
Girish
Title: Reply   
Name: rakesh
Date: 2005-05-09 1:20:04 AM
Comment:
too good
Title: Great article!   
Name: Hui
Date: 2005-05-05 12:05:57 AM
Comment:
Hi Souri,

Great article, and thanks for sharing this!
It has helped me to meet client requirements and amazed a few of my work colleges!

Thanks again!
Title: Excelant   
Name: Kaushal G. Visani
Date: 2005-05-03 4:26:43 AM
Comment:
That is nice,
thanks for yr that kind help
it is very useful me.
i also inspire to send my open source to u
if i create anything new i send it to u

Email : kaushal.mca@gmail.com

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-22 6:27:51 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search