问题描述
我有 DateTimeExpired 列,我想创建另一个名为Expired"的列,它将根据到期日期显示是"或否" - 如果日期已经过去,则显示是".
我是这样写的:
I have the column DateTimeExpired, and I would like to create another column called "Expired" which will show "Yes" or "No" according to the expiration date - "Yes" if the date has already passed.
I wrote this:
DataColumn colExpirationDate = new DataColumn("DateTimeExpired", typeof(DateTime));
DataColumn colExpired = new DataColumn("Expired", typeof(string), "IIF(DateDiff(DateTimeExpired, date())>= 0,'No','Yes')");
但我得到一个异常表达式包含未定义的函数调用 DateDiff()."
But I get an exception "The expression contains undefined function call DateDiff()."
(请注意,我总是想获取该行,无论它是否过期)
(please note that I always want to get the row, no matter if it's expired or not)
如何将列的文本设置为正确的形式?
How do I set the column's text to the correct form?
推荐答案
正如 MA Hanin 在评论中指出的那样,DateDiff 似乎不能在 数据列表达式.您可以尝试将计算列构建到基础表中(如果您使用的是 MS Sql 或类似的)
As M.A Hanin pointed out in his comment, it looks like DateDiff cannot be used in DataColumn Expressions. You could try building the calculated column into the underlying table instead (if you are using MS Sql or similar)
没有获取今天"的功能,但假设您添加的 DataColumn 仅存在几个小时,您可以将今天的日期构建为常量,然后使用比较运算符而不是 DateDiff
edit: There is no function to get 'today', but assuming that the DataColumn you are adding will only exist for a few hours, you could build in todays date as a constant, and then use comparison operators instead of DateDiff
试试这个:
DataColumn colExpirationDate = new DataColumn("DateTimeExpired", typeof(DateTime));
DataColumn colExpired = new DataColumn("Expired", typeof(string),
String.Format("IIF(DateTimeExpired > #{0}#,'No','Yes')",
DateTime.Now.ToString("dd/MMM/yyyy")));
请注意,这仅在您的 DataColumn 仅在内存中保留不到一天时才有效.
Note this will only work if your DataColumn is only retained in memory for less than a day.
这篇关于为数据表中的计算列定义 DateDiff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!