排序日期列gridview [英] sort on date column gridview
问题描述
我有一个自动生成列的gridview。其中一列是日期字段。
不知何故,我无法排序日期字段。它被解释为一个字符串。
数据出来一个数据库。数据类型在datetime上设置。
这是aspx文件中的代码:
< asp:BoundField DataField =dateHeaderText =dateSortExpression =dateDataFormatString ={0:dd / MM / yyyy}HtmlEncode =false/>
我在这里做错了什么?
我知道这是一个相对较旧的帖子,但我自己寻找一个解决方案,我想这一点。
如果 Date
被填充为字符串,那么您需要确保它不是。要做到这一点为表格创建数据为 DataTable
,并在添加数据时使用:
dt.Columns.Add(DateTime,System.Type.GetType(System.DateTime));
这会按照您的意愿对日期列进行排序。
I got a gridview which auto generates columns. One of the columns is a date field. Somehow, i cant sort the date field. It's being interpreted as a string.
The data comes out a database. The datatype there is set on datetime.
This is the code in the aspx file:
<asp:BoundField DataField="date" HeaderText="date" SortExpression="date" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="false"/>
What am i doing wrong here?
I realise this is a relatively old post but looking for a solution myself I figured this out.
If the Date
is being populated as a string then you need to make sure it's not. To do this create the data for the table as a DataTable
and when adding in the data use:
dt.Columns.Add("DateTime", System.Type.GetType("System.DateTime"));
This sorts the Date column out just as you would like it.
这篇关于排序日期列gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!