Just want to correct the @Anuj answer, which works great for me.
In the definition of the DataTable
object, you need to specify the type of the object when you add column, like this:
DataTable dataTable = new DataTable("Log");
dataTable.Columns.Add("Start", typeof(DateTime));
dataTable.Columns.Add("End", typeof (DateTime));
And then, in the code behind of your ASP page, use this:
private string SortField
{
get { return (string) ViewState["SortPropertyName"]; }
set { ViewState["SortPropertyName"] = value; }
}
private string SortDirection
{
get { return (string) ViewState["SortDirection"]; }
set { ViewState["SortDirection"] = value; }
}
And in your SortCommand method:
if (SortField.Equals(e_.SortExpression))
SortDirection = SortDirection == "asc" ? "desc" : "asc";
else
{
SortDirection = "asc";
SortField = e_.SortExpression;
}