【发布时间】:2015-01-08 15:17:25
【问题描述】:
我希望 DataGrid 中的一列始终以空值排序。 我试图通过关注this (part 1) 和this (part 2) 来做到这一点。但是我的自定义排序不像我想要的那样工作。
这就是我的专栏比较方法:
private int NullableDateTimeCompare(DateTime? date1, DateTime? date2, int direction)
{
if (date1.HasValue && date2.HasValue)
return DateTime.Compare(date1.Value, date2.Value)*direction;
if (!date1.HasValue && !date2.HasValue)
return 0;
if (date1.HasValue)
return 1 * -direction; // Tried different things but nothing work like I will
return -1 * -direction; // Tried different things but nothing work like I will
}
我的印象是这不起作用,因为 DataGrid 缓存了比较结果,因此在用户排序时会反转排序(并且不要再次运行比较)。
你知道怎么做吗?
谢谢!
【问题讨论】:
-
如果您总是希望底部有空值,那么空值和非空值之间的比较不应受方向影响。
标签: c# wpf sorting datagrid compare