在两个字符串之间绑定source.filter VB.net

时间:2018-06-28 00:34:46

标签: vb.net filter bindingsource

如何过滤两个字符串之间的绑定源。 我尝试了以下方法:

?

但是结果不包括value2。 我想不出另一种方法。

1 个答案:

答案 0 :(得分:1)

实际上,仔细查看您的代码,如果您发布的内容实际上是您正在使用的内容,那么我认为我可以看到问题。您在第一个单引号之后紧跟一个空格,在最后一个单引号之前紧跟另一个空格。这个:

BindingSource.Filter = "[field]>= ' " & value1 & "' and [field] <= '" & value2 & " ' "

实际上应该是这样的:

BindingSource.Filter = "[field]>= '" & value1 & "' and [field] <= '" & value2 & "'"

这是一个为什么要使用String.Format或字符串插值的完美示例,因为使用多个&运算符会使代码的可读性降低,因此更容易出错:

BindingSource.Filter = String.Format("[field] >= '{0}' and [field] <= '{1}'", value1, value2)

或:

BindingSource.Filter = $"[field] >= '{value1}' and [field] <= '{value2}'"