vb.net 绑定源过滤器将日期转换为字符串

发布于 2024-10-06 20:11:22 字数 752 浏览 10 评论 0原文

我正在使用 过滤器 方法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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴骨ら 2024-10-13 20:11:22

我明白了,它有效

bindingSource.Filter = "ProductId LIKE '%" & searchterm & "%' OR Convert( ScanDate, 'System.String') LIKE '%" & searchterm & "%'"

I got it, and it works

bindingSource.Filter = "ProductId LIKE '%" & searchterm & "%' OR Convert( ScanDate, 'System.String') LIKE '%" & searchterm & "%'"
深海夜未眠 2024-10-13 20:11:22

您应该分解您的过滤器代码。因此,我不会将过滤器代码全部设置在一行中,而是会对 searchterm 运行测试以查看它是否是有效的 DateTime。然后,您可以相应地更改过滤器,因为我认为您不会拥有类似于 DateTimeProductId

try
{
    string dt = DateTime.Parse(searchterm).ToString();
    filter = "ScanDate like '%" & searchterm & "%'"
}
catch
{
    filter = "ProductId LIKE '%" & searchterm & "%'"
}

对 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 valid DateTime. Then, you can change your filter accordingly because I don't think you'll have a ProductId which is like a DateTime.

try
{
    string dt = DateTime.Parse(searchterm).ToString();
    filter = "ScanDate like '%" & searchterm & "%'"
}
catch
{
    filter = "ProductId LIKE '%" & searchterm & "%'"
}

Sorry for the C# code, but it should be straight forward to convert to VB.Net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文