简体   繁体   English

比较DataView.RowFilter中的日期?

[英]Compare dates in DataView.RowFilter?

I am scratching my head over something rather stupid yet apparently difficult. 我正在摸索一些相当愚蠢而又显然很难的东西。

DataView dvFormula = dsFormula.Tables[0].DefaultView;
dvFormula.RowFilter = "'" + startDate.ToString("yyyyMMdd") + "' < EndDate OR EndDate = '19000101'";
dvFormula.Sort = "FromDate ASC";

The result is this: 结果是这样的:

Cannot perform '<' operation on System.String and System.DateTime. 无法对System.String和System.DateTime执行'<'操作。

Please tell me what the best way to solve this problem would be. 请告诉我解决这个问题的最佳方法是什么。

Much appreciated! 非常感激!

你需要用#包装日期,而不是撇号。

dvFormula.RowFilter = "#" + startDate.ToString("MM/dd/yyyy") + "# < EndDate OR EndDate = #1/1/1900#"; 

This is the solution. 这是解决方案。 Try this: 尝试这个:

filter = " (Date >= #" +
         Convert.ToDateTime(txtFromDate.Text).ToString("MM/dd/yyyy") +
         "# And Date <= #" +
         Convert.ToDateTime(txtToDate.Text).ToString("MM/dd/yyyy") +
         "# ) ";

Depending on your data provider, you may need to escape dates with the # character rather than the ' character. 根据您的数据提供者,您可能需要使用#字符而不是'字符来转义日期。 In addition, I would format your dates in the format YYYY-MM-DD to ensure it can be recognized as a date correctly. 另外,我会以YYYY-MM-DD格式格式化您的日期,以确保它可以被正确识别为日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM