Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1089
  • Last Modified:

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.
0
Eamon
Asked:
Eamon
  • 6
  • 4
  • 2
2 Solutions
 
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
 
garavindbabuCommented:
You can even change your query or SP which u r binding to have desc as default behaviour.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now