Finding a String in a datagrid view.

Posted on 2007-10-16
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
    LVL 16

    Expert Comment

    try filtering on the dataset thats associated to the gridview

    Author Comment

    how do I do that?
    LVL 22

    Expert Comment

    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.


    Author Comment


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


    Author Comment

    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

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now