[Last Call] Learn how to a build a cloud-first strategyRegister Now


Finding a String in a datagrid view.

Posted on 2007-10-16
Medium Priority
Last Modified: 2013-11-26
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.
Question by:wilecoyte78
  • 3
  • 2
LVL 16

Expert Comment

ID: 20089288
try filtering on the dataset thats associated to the gridview

Author Comment

ID: 20089298
how do I do that?
LVL 22

Expert Comment

ID: 20089362
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.

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.


Author Comment

ID: 20089443

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


Author Comment

ID: 20089474
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")))
LVL 22

Accepted Solution

JimBrandley earned 2000 total points
ID: 20089551
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.


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question