GridView Sort DESC

Eamon
Eamon used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dirk HaestProject manager
Commented:
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

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

You can even change your query or SP which u r binding to have desc as default behaviour.
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Dirk HaestProject manager

Commented:
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

Author

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.
Dirk HaestProject manager

Commented:
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

Author

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
Dirk HaestProject manager

Commented:
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...

Author

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

Author

Commented:
sorry but what I used seems to sort desc only. will post again when I sort it.

Author

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

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial