Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I want to sort my GridView when the webpage
loads up.

At the moment the user needs to click on the
Headers text.

I want that to happen automattically?

Any help would be appriciated!!
Posted
Comments
saini arun 28-Nov-11 5:27am    
If you want the grid view sorted on page load, why don't you bind it with sorted data on the first place?

This can be done at the database level or you can sort the data set on a particular column and then bind it to grid view.

Ben Paxton 28-Nov-11 5:34am    
Yes I know that! That is what if have already done, but I want to only view the last 144 rows in the DB table thats why I sorted it desc top(144) and now I want to set it back to asc
saini arun 28-Nov-11 5:52am    
oops, sorry I should have guessed that you already have the records in ascending order and now want to display them in descending order.

:)

Try this one:

C#
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 GridView_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();         
}


also, add SortExpression="<value here="">" in your client side script and AllowSorting=True to your gridview property.

For your reference:
http://stackoverflow.com/questions/702600/sorting-and-paging-with-gridview-asp-net[^]

Please mark as answer if this solved your problem.

-Eduard
 
Share this answer
 
v3
Comments
D K N T H 28-Nov-11 5:42am    
hey you hit a nerve boy,,five for that! haha
[no name] 28-Nov-11 19:55pm    
thanks men :D
Ben Paxton 28-Nov-11 5:46am    
I have error on GetData. What should I declare to get rid of this?
Ben Paxton 28-Nov-11 6:05am    
I only have one error left at the last method where the dataTable is declared. Error at GetData()
[no name] 28-Nov-11 19:55pm    
rename the GetData() with your method that returns and binds data, a dataset to be particular
sort the gridview data in its query it self and load the gridview in page load event.

Hpoe this helps
 
Share this answer
 
i think Eduard Lu got it, we have the same solution and it works good, i think this is what you need,

hope it helps!

thanks,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900