wilecoyte78
asked on
Finding a String in a datagrid view.
I have this code to search a datagridview:
Private Sub FindToolStripMenuItem_Clic k(ByVal sender As Object, ByVal e As System.EventArgs) Handles FindToolStripMenuItem.Clic k
SearchString = UCase(Trim(InputBox("Searc h 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.FirstDisplay edScrollin gRowIndex = 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.
Private Sub FindToolStripMenuItem_Clic
SearchString = UCase(Trim(InputBox("Searc
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.FirstDisplay
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.
try filtering on the dataset thats associated to the gridview
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
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
ASKER
Jim,
I did a test and both values equal each other but the if statement is returning a false.
Ben
I did a test and both values equal each other but the if statement is returning a false.
Ben
ASKER
I found out the problem
in my code I have SearchString = UCase(Trim(InputBox("Searc h 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("Searc h for", "Find")))
in my code I have SearchString = UCase(Trim(InputBox("Searc
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("Searc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.