Julio Romero
Julio Romero

Reputation: 35

Binding source.filter between two strings VB.net

How can I filter a binding source between two strings. I've tried the following:

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

But the result doesn't include the value2. I can't think of another way to do it.

Upvotes: 1

Views: 1319

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

Actually, looking at your code more closely, if what you posted is actually what you're using then I think I can see the issue. You have a space immediately after the first single quote and another immediately before the last one. This:

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

should actually be this:

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

This is a perfect example of why you should use String.Format or string interpolation because using multiple & operators makes the code less readable and thus more error-prone:

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

or:

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

Upvotes: 1

Related Questions