Hi, using VB 2008, I have a datagridview bound to a datasource and want to position to a row based on the value keyed by the user in a textbox. I can sort of accomplish this with one line of code on the textbox changed event i.e.:
Me.BindingSource.Position = Me.BindingSource.Find("ColumnName", TextBox1.Text)
which works great when the user keys in an exact match. Question is, how do I position to a partial match, i.e. the first match found, i.e. "ABC " might position to "ABCDEFG" if it is the first record alphabetically. I can't seem to find a decent example of how to do this.
Bill P.

8 Years
Discussion Span
Last Post by Bill Purkins

I worked on something like this a little while ago, we used the filter & sort propery to narrow down the list.

Me.BindingSource.Filter = "Column LIKE '" & Textbox.Text & "&'"
Me.BindingSource.Sort = "Column Desc"

I think it's
Me.BindingSource.Filter = "Column LIKE '%" & Textbox.Text & "%'"

Opps, A typo on my part...:$


Thanks, this looks like it will work. In the interim, I have another solution which I came up with and will stick with in this case, as if it ain't broke, etc...
What I did was loop through the rows programattically and see if the first cell contents was in the range of what the user typed in the textbox field with a whole bunch of 99999's concatenated to it, with the result of positioning the datagridview at the first row that matched that fell in that range. It seems kind of a hokey way to do it, but it preserves the datagridview list as a whole where I think the filter approach would present a subset of the list, which I don't really want.

Dim i As Integer ' index for loop
        Dim r As Integer ' for retrieve row count 
        Dim UpperLimitString As String ' upper range limit
        Dim cellvalue As String ' cell value retrieved
        UpperLimitString = CStr(TextBox1.Text + "99999") ' set upper limit
        r = DoctorsDataGridView.RowCount ' get row count
        r = r - 2 ' adjust for 0 index and blank row at bottom

        For i = 0 To r 'loop through all rows 

            cellvalue = ""
            cellvalue = CStr(DoctorsDataGridView(0, i).Value)

            ' 1st cell value within range of keyed value & upper limit?
            If CStr(cellvalue.ToString) >= (CStr(TextBox1.Text)) And (CStr(TextBox1.Text) <= CStr(UpperLimitString.ToString)) Then
                Me.DoctorsBindingSource.Position = Me.DoctorsBindingSource.Find("Doctor", cellvalue)
                GoTo GotMyRecord
            End If
        Next i
        Me.DoctorsBindingSource.Position = Me.DoctorsBindingSource.Find("Doctor", cellvalue)


Thanks for your help. I'm putting both methods in my toolbox.
Bill P.


In playing around, I tried filter with a wildcard on a new textbox field and it works great:

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

        DoctorsBindingSource.Filter = "Doctor like '%" + TextBox2.Text + "%'"

End Sub
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.