We know that after creating a sortable GridView, click the column header, and the column will be in a descending order (SortDirection. ASC) sort, and then click this column in the order from large to small (SortDirection. DESC) sorting.
How can this process be reversed, that is, by default, the first sorting is performed in a descending order? Unfortunately, the GridView does not provide anything like DefaultSortDirection for us to set, so we must use the code:
<Asp: GridView runat = "server" id = "GridView1" AllowSorting = "True" OnSorting = "GridView1_Sorting">
</Asp: GridView>
Protected void gridviewinclusorting (object sender, GridViewSortEventArgs e)
{
// Obtain the name of the sorted Field
String field = e. SortExpression. Split ('') [0];
// If this is the first time this field is sorted
If (ViewState ["previussortfield"] as string! = Field)
{
// Change the order from large to small
E. SortExpression = e. SortExpression + "DESC ";
// Record the name of this field (next time it is not the first time)
ViewState ["previussortfield"] = field;
}
}