Finding a String in a datagrid view.

I have this code to search a datagridview:

    Private Sub FindToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FindToolStripMenuItem.Click
        SearchString = UCase(Trim(InputBox("Search for", "Find")))
        Dim datarowcount = DataGridView1.RowCount
        Dim x = 0
        For x = 0 To datarowcount - 1
            If DataGridView1.Item(1, x).Value.ToString = SearchString.ToString Then
                DataGridView1.Item(1, x).Selected = True
                DataGridView1.FirstDisplayedScrollingRowIndex = x
            End If
    End Sub

The field it is searching is an URL field. I did a test and if there are no charachers just numbers for example all the URL Field had was 1 through 12 and I do a search for 9 it will go to it, but if I have any text whatsoever it will not find any query I put in. I even did a mix of numbers and characters it would only find the file that had the lone number in it.
Who is Participating?
JimBrandleyConnect With a Mentor Commented:
I saw that, but though you did it to match the content of the DataGridView. Next time, check out string.Compare. You can tell it to ignore case. It is faster too.

try filtering on the dataset thats associated to the gridview
wilecoyte78Author Commented:
how do I do that?
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

I strongly suspect something isn't returning what you think is it. Define a string outside the loop, say rowValue. Then inside the loop, set rowValue = DataGridView1.Item(1, x).Value.ToString  before you compare the values. Set a breakpoint on:
If DataGridView1.Item(1, x).Value.ToString = SearchString.ToString Then

When you hit the breakpoint, Quickwatch SearchString and rowValue. That should provide a clue.

wilecoyte78Author Commented:

I did a test and both values equal each other but the if statement is returning a false.

wilecoyte78Author Commented:
I found out the problem

in my code I have SearchString = UCase(Trim(InputBox("Search for", "Find")))

Since I have that code I have to make sure everything is Uppercase, but everything is lowercase so I just changed it to:

SearchString = LCase(Trim(InputBox("Search for", "Find")))
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.