Link to home
Start Free TrialLog in
Avatar of wilecoyte78
wilecoyte78

asked on

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
        Next
    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.
Avatar of SQL_SERVER_DBA
SQL_SERVER_DBA
Flag of United States of America image

try filtering on the dataset thats associated to the gridview
Avatar of wilecoyte78
wilecoyte78

ASKER

how do I do that?
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.

Jim
Jim,

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

Ben
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")))
ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial