添加 Rowfilter 条件仅用于 DateTime Year 检查
我正在尝试使用 DataView.RowFilter 来过滤不属于指定年份的所有条目。我的代码如下:
bigDT.DefaultView.RowFilter = "year(date_posted)=2011";
但是,这不起作用。我读过,我可以使用“#mm/dd/yyyy#”等格式指定日期时间。
我希望只检查年份,因为用户可以指定年份,也可以不指定年份、月份或日期等。
谢谢。
I am trying to use a DataView.RowFilter in order to filter all entries that do not belong in a specified year. My code is as follows:
bigDT.DefaultView.RowFilter = "year(date_posted)=2011";
This however, does not work. I have read that I can specify DateTimes using a format like "#mm/dd/yyyy#".
I'd prefer if I could check only the year, as users can specify a year, or no year, or month, or day, etc.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DateTime 的有效表达式。
你可以做类似的事情
Valid expressions for DateTime.
You can do something like
我并不是说这是一个好方法,但您可以这样做的一种方法是:
将
date_posted
列转换为字符串,提取四个-数字日期,将其转换为整数,并将其与2012
进行比较。CONVERT
和SUBSTRING
函数记录在DataColumn.Expression
属性。文档没有说,但我想
CONVERT(date_posted, 'System.String')
使用适合您当前文化的短日期模式,所以在我的情况下 (en-US) 年份是在字符偏移量 5-8 处。 Anurag Ranjhan 的答案 肯定更好,因为它非常冗长并且依赖于解析格式化字符串,但这是另一种方法它确实有效。I'm not saying this is a good way to do it, but a way you can do it is this:
That's converting the
date_posted
column to a string, extracting the four-digit date, converting that to an integer, and comparing that for equality to2012
. TheCONVERT
andSUBSTRING
functions are documented in theDataColumn.Expression
property.The documentation doesn't say, but I imagine that
CONVERT(date_posted, 'System.String')
uses the short date pattern for your current culture, so in my case (en-US) the year was at character offsets 5-8. Anurag Ranjhan's answer is certainly better given how verbose this is and its reliance on parsing formatted strings, but this is another way to do it and it does work.确保文档的列名称为“year”,并且该列中的字段是文本或日期时间。如果是日期时间,则需要具体,可能包括日期、时间等。如果是文本,则应为defaultview.rowfilter =“year = 2011”。
Make sure the document has the column name "year" and the field in that column is a text or datetime. If it is datetime, you need to be specific and probably include the date, time, etc. If it is text it should be defaultview.rowfilter = "year = 2011".