Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

We all know that GridView columns can be sorted automatically when SQLDataSource is being used. But what if you are not using SqlDataSource to populate the GridView. Sorting the GridView manually is pretty straight forward task take a look at the code below.

 private const string ASCENDING = " ASC";
 
private const string DESCENDING = " DESC";

 public SortDirection GridViewSortDirection
    {
        
get
        
{
            
if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            
return (SortDirection) ViewState["sortDirection"];                
        }
        
set { ViewState["sortDirection"] = value; } 
    }


    
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        
string sortExpression = e.SortExpression;

        
if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, DESCENDING);
        }
        
else
        
{
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, ASCENDING); 
        }   
       
    }

    
private void SortGridView(string sortExpression,string direction)
    {
        
//  You can cache the DataTable for improving performance
        
DataTable dt = GetData().Tables[0]; 

        DataView dv = 
new DataView(dt); 
        dv.Sort = sortExpression + direction;         

        GridView1.DataSource = dv;
        GridView1.DataBind();         
    }

Hope it helps!

powered by IMHO 1.3

Posted on Thursday, April 27, 2006 9:13 PM | Back to top


Comments on this post: Sorting GridView Columns Manually

# re: Sorting GridView Columns Manually
Requesting Gravatar...
euh... what's "GetData" ?
Left by zar on May 16, 2006 10:31 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Tnx, For 5 hours I was trying to do it.
Left by Ohad on May 24, 2006 4:56 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
private DataSet GetData()

{

SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories", myConnection);

DataSet ds = new DataSet();

ad.Fill(ds);

return ds;

}
Left by azamsharp on Jun 06, 2006 3:20 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
sure helps out!

really good code!

ps. does this also work when paging is enabled?
Left by JP on Jun 07, 2006 8:08 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Yeah, but what if we don't want to re-query the database? there should be a way we can retrieve the databound source and re-create the dataset/dataview without re-hitting the database.

I assumed the 'sender' (first param) on the sorting method would be a copy of the gridview that fired the event, but it isn't.

Any ideas?
Left by Ed on Jun 09, 2006 6:55 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
You can always Cache the data container and avoid a trip to the database.
Left by AzamSharp on Jun 09, 2006 7:16 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
good code, it helped me a lot
Left by Goldy on Dec 28, 2006 12:28 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
two calls in GridView1 method was ocorrs in some one click in header gridview... what happened?
Left by Ivan on Mar 10, 2007 8:44 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
This code is very simplified and very useful for me
Left by Satish on Sep 10, 2007 1:54 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
its working fine but i click edit button in gridview sorting changed to reverse order.
Left by Murugan on Oct 12, 2007 1:35 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
my grdiview bind with data table (tb); in asp:templatefield's header temple i have a link button thread. now i want that when we click on thread then sorting should be going.

how could be possible. plz reply


its very urgent.
Left by Ranjan on Oct 17, 2007 2:26 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
i am getting an error saying the name 'ConnectionString' does not exist in the current context
Left by s.k.chaitanya on Dec 07, 2007 12:14 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Its great ..... AND awesome.. but am facing the same problem faced by Murugan..when i click edit button in gridview sorting changed to reverse order. It would be great if you or anyone in this boll helps me out
Left by kashif on Dec 10, 2007 7:46 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Simple & Useful
Left by vijila on Feb 01, 2008 4:54 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Good code
Left by Dheeraj on Feb 06, 2008 10:24 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Do you know others site about Sorting GridView Columns Manually ??

this site is very good
Left by Marek on Feb 07, 2008 8:29 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Sorting is working fine.But it is doing sort in descending order only
Left by sivasree on Feb 13, 2008 12:09 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Thanks Dude This code is helped me Lot...
Good coding
Left by Anand on Mar 18, 2008 7:49 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
nice code. But i would like to sort gridview either by clicking a column link or selecting a column from a dropdown outside the gridview.
is that possible?
Left by Menaka on Apr 02, 2008 2:18 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Dear,

At below line I am getting error says column not found because we concentating the strings.

DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;

It was not helpful to me and Look likes it sort in descending order only..

Left by Sachin on Apr 02, 2008 6:08 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Excelente, me ayudo mucho
Left by Klau1205 on Apr 03, 2008 7:24 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Thank you very boss. i tried with your code . Column wise sorting is perfectly working.
sivakumar.aruchamy
Left by sivakumar aruchamy on Apr 06, 2008 9:50 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
it's work!
Left by RDEJAN on May 19, 2008 1:11 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Excellent! Thank you so much!!!
Left by sunnyside on Jun 19, 2008 6:25 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
hi.
how to sort & search in gridview manually.. and one more thing is i want to show the data in same gridview.but occurred connection was not closed error. plz i need example.....
Left by kannan on Jun 21, 2008 5:56 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Can U Please Tell..How Searching is done in the Gridview...
Left by Vikram on Aug 16, 2010 4:15 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Really Excellent... It helps me a lot...
Good...
Left by Satheesh on Sep 28, 2010 6:46 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
this information's very good, thanks
Left by wiwat on Oct 13, 2010 1:54 AM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
But i am use <TemplateField>.Then command name fire when i click the likbutton that is place on <HeaderTemplate>.So how this work
Left by Ramnjit singh on Oct 18, 2010 11:42 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Thanks for Sharing such a valuable piece of code.. It helped me a lot.. thanx...

Left by Rupendra Singh on Oct 31, 2010 10:49 PM

# re: Sorting GridView Columns Manually
Requesting Gravatar...
Code is working.......
Thanks a lot......
Left by nirav on Nov 30, 2010 9:35 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net