How may I filter a bindingsource between 2 numbers? Obviously "column_name BETWEEN Num1 AND Num2" does not work, since between is nor supported. Any ideas?

Edited by ktab: n/a

5 Years
Discussion Span
Last Post by ktab

Also another question. Is it better to connect (datasource) my datagridview with a dataview or a bindingsource?


first add a datagridview to your form then select add query
than which ever the filed you want to search or do whatever select that or go in querybuilder in that specific filed filter write LIKE @ fieldname + '%'

hope this helps!!!! :)


Thanx for you response, but this is not actually what I was asking for. I know how to filter bindingsource for strings, dates (even the between operator) or numbers.
My problem is that I don't know how to filter between 2 numbers.

>=NUM1 AND NUM2=< is the closest I can think of, but if user types numbers in other way it will not be between but exclude :/
I may fix it with a small piece of code so Num1 always gets smallest value of 2 and Num2 the bigger, but I was thinking of a more "clean" solution...

sub filterBS
                If isMaxNumber(tbMaster.Text, tbSlave.Text) Then
                    value2 = tbMaster.Text
                    value1 = tbSlave.Text
                    value2 = tbSlave.Text
                    value1 = tbMaster.Text
                End If

Dim strFilter As String = String.Format("{2} >= {0} AND {2} <= {1}", value1, value2, [column_name])
'string for dates: "{2} >= #{0:M/dd/yyyy}# AND {2} <= #{1:M/dd/yyyy}#"

end sub

    Private Function isMaxNumber(ByVal TargetNumber As String, ByVal SecondaryNumber As String) As Boolean
        Dim x As Double, z As Double, result As Boolean
        Double.TryParse(TargetNumber, x)
        Double.TryParse(SecondaryNumber, z)

        result = x > z
        Return result
    End Function

    Private Function isMaxDate(ByVal TargetDate As String, ByVal SecondaryDate As String) As Boolean
        Dim x As DateTime, z As DateTime, result As Boolean
        DateTime.TryParse(TargetDate, x)
        DateTime.TryParse(SecondaryDate, z)

        result = x > z
        Return result
    End Function

The solution that I came up with... Now, whatever value is in 1st or second textbox, the "between" will not be missed...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.