筛选日期范围-从日期到日期-数据表 [英] Filter Date Range - from Date to Date - DataTable

查看:114
本文介绍了筛选日期范围-从日期到日期-数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#比较陌生,但是正在学习。我有一个带有名为Returns的表的DGV,而Date列为DateOfEntry。我以为设置起来会很简单..但是我一直收到Operand错误,但我不确定为什么。我对此进行了无止境的研究,并尝试了各种方法,但都没有碰到运气。以下是我使用的代码,有人可以向我解释我可能做错了什么吗?

I am relatively new to C#, but learning. I have a DGV with a table called Returns and the Date column is DateOfEntry. I thought setting this up would be rather simple..but I keep getting Operand errors and I am not sure why. I have researched this to no end and have tried various approaches with no luck. Below is the code I am using, could someone explain to me what I may be doing wrong?

使用和访问.mdb文件作为数据库

Using and Access .mdb file as a DB

returnsBindingSource.Filter = ("Select * from Returns where DateOfEntry 
between '"+dateTimePicker1.Value.ToString()+"' and  
'"+dateTimePicker2.Value.ToString()+"'");


推荐答案

您不应将select语句分配为 BindingSource过滤器 。它需要遵循以下表达式语法

You should not assign a select statement as BindingSource.Filter. It needs a string filter expression which follows these expression syntax.

实际上,这是一种客户端过滤机制,不需要往返数据库服务器的过滤机制。当您将过滤器表达式分配给 BindingSource.Filter 时,过滤器将应用于基础数据源,例如,应用于您的 DataTable

In fact it's a client-side filter mechanism which doesn't need a round-trip to data base server to filter data. When you assign a filter expression to BindingSource.Filter, the filter will apply on underlying data source, for example on your DataTable.

因此,您应该在表单的 Load 事件中加载数据,然后将过滤器应用于例如,点击事件是一个过滤器按钮。

So you should load data in Load event of the form, then apply the filter on loaded data in Click event of a filter button for example.

日期值应包含在#号内。例如:

Date values should be enclosed within # signs. For example:

returnsBindingSource.Filter =
    String.Format("DateOfEntry >= #{0:yyyy/MM/dd}# AND DateOfEntry <= #{1:yyyy/MM/dd}#",
    dateTimePicker1.Value,
    dateTimePicker2.Value);

这篇关于筛选日期范围-从日期到日期-数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆