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

Binding source.filter between two strings VB.net

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

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

但是结果不包括value2。 我想不出其他方法。

实际上,更仔细地查看您的代码,如果您发布的实际上是您正在使用的代码,那么我想我可以看出问题所在。您在第一个单引号之后有一个 space,在最后一个单引号之前有另一个。这个:

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}'"