GridView Sort DESC

When I click on a column in my grid view it sorts ASC. Is there anyway to get my gridviews to sort DESC by default. Currently the user clicks it once to sort the column and then again to sort it DESC. I would need this to work for any column in the grid.
LVL 1
EamonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
You can customize this, see this example
GridView: default sorting
http://www.dotnetjunkies.com/WebLog/colt45/archive/2006/06/21/140956.aspx
[Browsable(true)]
[Description("Default sort direction")]
[Category("Behavior")]
public SortDirection DefaultSortDirection
{
    get
    {                
        object defaultSortDirection = this.ViewState["DefaultSortDirection"];
        if (defaultSortDirection == null)
        {
            return SortDirection.Descending;
        }
        return (SortDirection)defaultSortDirection;
    }
    set
    {
        this.ViewState["DefaultSortDirection"] = value;
    }
}

Open in new window

0
garavindbabuCommented:
write an event OnSorting:
protected void SearchResultsView_OnSorting( object sender, GridViewSortEventArgs e ) {
  e.SortExpression = e.SortExpression + " DESC"
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garavindbabuCommented:
You can even change your query or SP which u r binding to have desc as default behaviour.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

DhaestCommented:
To garavindbabu
OnSorting:
-> if you perform it that way, I think that it will be always sorted DESC and never ASC !
Altering SP: The user can click any column, so it's not possible to sort the SP. It can be used for the first time to order on a specific field to start from
0
EamonAuthor Commented:
Thanks Dhaest
My internet went down. You basically asked what I wanted to.

Can I check if that coulmn is already sorted DESC and then sort ASC.
I want DESC to be the default option, not the only option.
0
DhaestCommented:
It will be in both directions if you set it up like the link i've provided.
If there is no sorting yet, it will take the descending as default
0
EamonAuthor Commented:
Dhaest
I already have the grids on my forms and don't really want to go creating custom controls.

However your answer does seem to be the best solution so far as it would sort DESC by default
0
DhaestCommented:
You don't need to change your control. You're just extending your control.
Try to add the class into your project, add the properties in the article described, change your aspx-code and try it...
0
EamonAuthor Commented:
This is what I did. Seems to work

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
        If e.SortDirection = SortDirection.Descending Then
            e.SortDirection = SortDirection.Ascending
        Else
            e.SortDirection = SortDirection.Descending
        End If
 End Sub
0
EamonAuthor Commented:
sorry but what I used seems to sort desc only. will post again when I sort it.
0
EamonAuthor Commented:
Ok this is it.

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
        If GridView1.SortDirection = SortDirection.Descending Then
            e.SortDirection = SortDirection.Ascending
        Else
            e.SortDirection = SortDirection.Descending
        End If
 End Sub
0
EamonAuthor Commented:
And this makes sure it sorts Desc if you click a different column.

 If GridView1.SortExpression = e.SortExpression Then
            If GridView1.SortDirection = SortDirection.Descending Then
                e.SortDirection = SortDirection.Ascending
            Else
                e.SortDirection = SortDirection.Descending
            End If
        Else
            e.SortDirection = SortDirection.Descending
        End If
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.