我使用ASP.NET和C#。这是我的代码。如何在gridview中对日期列进行排序?
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();
}
我正在对所有列进行排序并正常工作。但是与日期栏一样,按照这个顺序(dd/mm/yyyy)。
- 30/11/2012
- 2012年10月12日
2012年9月10日
<asp:BoundField DataField="ReportedDate" HeaderText="Reported Date" SortExpression="ReportedDate" DataFormatString="{0:DD/MM/YYYY}" HtmlEncode="false" />
此列的数据类型是日期。
如何做到这一点?我做错了吗?
在您的代码“30/11/2012”中,其他日期被视为字符串,只有字符串排序正在发生。 – sajanyamaha
谢谢..我知道,但我不知道为什么字符串排序正在发生。我宣布这只作为日期类型..你可以给我任何提示? – Giri