The GridView already existing in the Session can be checked for the previous sort expression and direction so that adding them to ViewState is not necessary. Example below...
protected void MyGridView_OnSorting(object sender, GridViewSortEventArgs e)
{
DataTable sortingTable = Session["MyGridViewDataTable"] as DataTable;
if (sortingTable != null)
{
// Check the DataTable itself for the previous sort expression.
if (sortingTable.DefaultView.Sort.ToString() == (e.SortExpression.ToString() + " ASC"))
{
sortingTable.DefaultView.Sort = e.SortExpression + " DESC";
}
else // Handles cases where the previous sort expression was the current expression descending, another expression, or none at all.
{
sortingTable.DefaultView.Sort = e.SortExpression + " ASC";
}
MyGridView.DataSource = Session["MyGridViewDataTable"];
MyGridView.DataBind();
}
}
-Luridis