I am using ASP.NET and C#.This is my code.
private const string ASCENDING = "ASC";
private const string DESCENDING = "DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection) ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
public string SortExpression
{
get
{
if (ViewState["sortExpression"] == null)
ViewState["sortExpression"] = "JobNumber";
return ViewState["sortExpression"] as string;
}
set { ViewState["sortExpression"] = value; }
}
protected void OnSorting(object sender, GridViewSortEventArgs e)
{
SortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
}
else
{
GridViewSortDirection = SortDirection.Ascending;
}
BindGrid();
}
I am applying sorting for all the columns and working fine. But with date column it is like in this order(dd/mm/yyyy).
- 30/11/2012
- 10/12/2012
-
9/10/2012
<asp:BoundField DataField="ReportedDate" HeaderText="Reported Date" SortExpression="ReportedDate" DataFormatString="{0:DD/MM/YYYY}" HtmlEncode="false" />
the datatype of this column is date.
How to do this?Am i doing wrong?
There are two options
1.To sort at SQL Level,this is the best and proper method,bind the resulting resultset in to gridview.
2.To bind a DataTable with the query output and run a sort on datatable,and then bind it to gridview.
keep in mind to add datatype to datatable column
accTable.Columns.Add("Date",typeof(DateTime));