如何在GridView中的日期列排序? [英] How to sort the date column in gridview?

查看:86
本文介绍了如何在GridView中的日期列排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET和C#.这是我的代码.

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();
    }

我正在对所有列进行排序,并且工作正常.但是,对于日期列,它按此顺序排列(dd/mm/yyyy).

I am applying sorting for all the columns and working fine. But with date column it is like in this order(dd/mm/yyyy).

  • 2012年11月30日
  • 2012年10月12日
  • 2012年9月10日

  • 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?

推荐答案

有两个选项

1.要在SQL级别进行排序,这是最好的方法,将结果集绑定到gridview.

1.To sort at SQL Level,this is the best and proper method,bind the resulting resultset in to gridview.

2.要将DataTable与查询输出绑定并在datatable上运行排序,然后将其绑定到gridview.请记住将数据类型添加到数据表列 accTable.Columns.Add("Date",typeof(DateTime));

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));

这篇关于如何在GridView中的日期列排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆