C#试图在RowFilter(报告)中按日期过滤

时间:2016-03-15 09:37:32

标签: c# .net filter datatable reporting

我试图按两个日期时间选择器中选择的日期进行过滤。 当我调试程序时,它会以“访问冲突”代码退出

filter = "date > #"+desde_dateTimePicker.ToShortDateString()+"# AND date < #"+hasta_dateTimePicker.Value.ToShortDateString()+"#";

this.DataTable1TableAdapter.Fill(this.DataSet1.DataTable1);
this.DataSet1.DataTable1.DefaultView.RowFilter = filter;
DataTable1BindingSource.DataSource = this.DataSet1.DataTable1.DefaultView;
this.reportViewer1.RefreshReport();

如果我手动编写过滤字符串中的日期,则可以正常工作。

1 个答案:

答案 0 :(得分:0)

根据这里,您需要以特定方式格式化您的日期。

http://www.csharp-examples.net/dataview-rowfilter/

dataView.RowFilter = "Date = #12/31/2008#"          // date value (time is 00:00:00) 
dataView.RowFilter = "Date = #2008-12-31#"          // also this format is supported 
dataView.RowFilter = "Date = #12/31/2008 16:44:58#" // date and time value

dataView.RowFilter = String.Format(CultureInfo.InvariantCulture.DateTimeFormat,
                     "Date = #{0}#", new DateTime(2008, 12, 31, 16, 44, 58));

当您使用.ToString方法时,这依赖于您可能不正确的区域设置。