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.

Edited 5 Years Ago by renzlo: n/a

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?

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

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

Edited 5 Years Ago by Reverend Jim: n/a

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.

Edited 5 Years Ago by renzlo: n/a

This question has already been answered. Start a new discussion instead.