【问题标题】:DataGridView Sort issuesDataGridView 排序问题
【发布时间】:2014-08-19 05:15:35
【问题描述】:

所以我有一个名为 dtgQue 的数据网格视图,这里是我的列的属性。

'Setting the Properties for the Due Date
QueDueDate.Name = "DueDate"
QueDueDate.ValueType = GetType(Date)
QueDueDate.HeaderText = "Due Date"
QueDueDate.CellTemplate = New DataGridViewTextBoxCell

然后在我向该列添加一堆行之后,我再做一个

dtgQue.Sort(dtgQue.Columns(3), System.ComponentModel.ListSortDirection.Descending)

但它的排序不是 100% 正确的,例如这里是一组按排序方式排列的日期。 2014 年 9 月 8 日、2014 年 9 月 2 日、2014 年 9 月 15 日、2014 年 10 月 1 日如您所见,出于某种原因,它在 9/8 之后放置了 9/2,这是不正确的。

【问题讨论】:

  • 对应的DataGridViewColumn.ValueType是什么?
  • 正如@Neverhopeless 的回答所表明的那样,您可能在列中使用了不理想的数据类型,例如一个字符串,它应该是一个日期。看看这篇 MSDN 文章:msdn.microsoft.com/de-de/library/…

标签: .net vb.net sorting datagridview


【解决方案1】:

我的猜测是你应该使用:

QueDueDate.ValueType = GetType(string)
QueDueDate.CellTemplate = New DataGridViewTextBoxCell

编写自定义排序函数,每次DataGridViewTextBoxCell.Value都会从String数据类型解析为Date数据类型,然后用于比较。

或者:

QueDueDate.ValueType = GetType(Date)
QueDueDate.CellTemplate = New CalendarCell

在这里查看CalendarCell

如果数据是适当的类型,那么它应该可以正确工作/排序。

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 2011-06-11
    • 1970-01-01
    • 2016-07-09
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    相关资源
    最近更新 更多