vb.net 绑定源过滤器将日期转换为字符串
我正在使用 过滤器 方法VB.net 中的 绑定源 进行过滤根据搜索框中的文本生成 DataGridView。然而,此搜索的想法是,如果任何单元格包含文本,它就会显示一行。所以我的过滤器字符串最终看起来像这样:
filter = "ProductId LIKE '%" & searchterm & "%'" & " OR ScanDate like '%" & searchterm & "%'"
但是,当我尝试将过滤器放入过滤器属性时,它会抱怨,说它无法将日期列转换为文本以进行比较。
有没有办法告诉过滤器将日期时间单元格转换为字符串?
我正在考虑做的是在数据集中有一个隐藏列,其中包含日期的转换版本,我将告诉过滤器过滤该列。
这是我的分配代码:
bindingSource.Filter = filter
dgv.DataSource = bindingSource.DataSource
I'm using the filter method of Binding source in VB.net to filter results in a DataGridView based on the text in a search box. However, the idea of this search, is that it shows a row if any of the cells contain the text. So my filter string ends up looking like this:
filter = "ProductId LIKE '%" & searchterm & "%'" & " OR ScanDate like '%" & searchterm & "%'"
However, when I try to put the filter in the filter property, it complains, saying that it cannot convert the date column to text for the comparison.
Is there a way to tell the filter to cast the datetime cells to string?
What I'm considering doing is having a hidden column in the dataset that contains a casted version of the date, and I'll tell the filter to filter that column.
Here's my assign code:
bindingSource.Filter = filter
dgv.DataSource = bindingSource.DataSource
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我明白了,它有效
I got it, and it works
您应该分解您的过滤器代码。因此,我不会将过滤器代码全部设置在一行中,而是会对
searchterm
运行测试以查看它是否是有效的DateTime
。然后,您可以相应地更改过滤器,因为我认为您不会拥有类似于DateTime
的ProductId
。对 C# 代码感到抱歉,但应该可以直接转换为 VB.Net。
You should break down your filter code. So, rather than setting your filter code all in one line, I would run a test on the
searchterm
to see if it is a validDateTime
. Then, you can change your filter accordingly because I don't think you'll have aProductId
which is like aDateTime
.Sorry for the C# code, but it should be straight forward to convert to VB.Net.