Hi All,

I want to search specific range of rows in datagridview. I want to search the entire third row of my datagridview. How can I do that?

Here's my current code:

Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        For Each row As DataGridViewRow In DataGridView.Rows
            ' Skip the new row
            If row.IsNewRow Then Exit For
            ' Loop through all the cells in the current row
            For Each cell As DataGridViewCell In row.Cells
                ' Skip non-text cells
                Try
                    If cell.GetType IsNot GetType(DataGridViewTextBoxCell) Then Continue For
                    ' Search for our matching text
                    If cell.Value.ToString.Contains(findBox.Text) Then
                        ' Select the cell if we have a match
                        DataGridView.CurrentCell = cell
                        Exit Sub
                    Else
                    End If
                Catch ex As Exception
                    MessageBox.Show("String NOT found.", "Search for: " & findBox.Text)
                End Try
            Next
        Next
        ' If we get to this point, we didn't find anything
        MessageBox.Show("String NOT found.", "Search for: " & findBox.Text)
    End Sub

Thanks in advance.

You are finding the particular row or string in particular cell. what do you want to find out.

I am loading a CSV file in datagridview, I made a search box but I want to start the search in third row, is it possible?

anyone?

try counting the number of rows and have your for loop start at index 2 until end of row, when the index = row count

hi bluehangook629,

can you give me a sample code? thanks.

Assuming you have data in the given row (in the example, I use row 2 (first row is row zero), you can access all of the cells in that row by

For col As Integer = 0 To dgvTest.ColumnCount - 1
    MsgBox(dgvTest.Rows(2).Cells(col).Value)
Next

thanks Reverend Jim, I'll try it now.

Glad to help. If that is what you were looking for then please come back to this thread and mark it as solved.

Hi Reverend Jim,

How can I incorporate this code in my code above? Thanks always.

I'm not sure what you are asking. You see how to access the cells. What you do once you have a match is up to you. By the way, because you have already determined that the value in the cell is a string you don't have to convert. You can use it directly so cell.Value.ToString is redundant. You can just use cell.Value as a string.

In the given code of my first post, it searches what's typed on the search box and it highlights the cell containing the searched string. The problem with that code is it searches also the first and second row, what I want is, the search should start in the third row.

Then just do the checking with a row index of 2 (third row zero relative)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.