vb.net中gridview中的排序日期 [英] sorting date in gridview in vb.net
问题描述
您好,我在排序时遇到问题.
当您单击gridview中的date列时,它会按月份和年份对数据进行排序.
所以2010年12月1日
2012年12月2日
12-05-2011
我想按年/月/日排序
Hello, I have a problem with sorting.
when you click on the date column in the gridview it sort the data by month- dd- year.
so 12-01-2010
12-02-2012
12-05-2011
i want to sort by year/month/day
Protected Sub gvLookup_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
If DirectCast(ViewState("sortExpression"), String) <> e.SortExpression Then
ViewState("sortExpression") = e.SortExpression
ViewState("sortDirection") = "ASC"
Else
If DirectCast(ViewState("sortDirection"), String) = "ASC" Then
ViewState("sortDirection") = "DESC"
ElseIf DirectCast(ViewState("sortDirection"), String) = "DESC" Then
ViewState("sortDirection") = "ASC"
Else
If ViewState("sortDirection") Is Nothing Then
ViewState("sortDirection") = "DESC"
End If
End If
End If
intCurPageIndex = 0
我试过了,但是没用
< asp:boundfield datafield =日期" dataformatstring ="{0:MMMM d,yyyy}" htmlencode ="false"/>
i tried this but it didn''t work
<asp:boundfield datafield="Date" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />
推荐答案
绑定数据,那么您的数据表应该对该列使用日期字段,而不是字符串.
If you are binding the data, then your datatable should be using a Date Field for that column instead of as a string.
dt.Columns.Add("DateTime", System.Type.GetType("System.DateTime"));
然后您的排序应该知道这是一个日期并且可以立即使用.
您遇到的问题是您的应用程序正在将该值作为字符串而不是日期进行排序.
Then your sort should know that this is a date and work out of the box.
The issue you are having is that your application is sorting this value as a string instead of a date.
这篇关于vb.net中gridview中的排序日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!