Hello all,

This is my first time posting here so bear with me. I am looking for some help in regard to a project I am currently doing. I have a Datagrid view that I am looking to search all columns and all rows for the string (partial or not) in all rows. The code I am posting currently does this however, it does not iterate through all of the rows, I did find a way to highlight all instances of the string but that is not what I want. Therefore I want the user to be able to click enter and it highlight the row with the next instance of what is typed into the searchbox and right now I have yet to find a suitable solution. If anyone has done this can you please help me or let me know what I am doing wrong? Thank you in advance for your correspondence.

Private Function findPart(ByVal strSearch As String) As Boolean ', ByVal colName As String)

    Dim Found As Boolean
    Dim strToSearch As String = strSearch.Trim.ToUpper
    Dim curRowInt As Integer = 0
    'Dim row As DataGridViewRow 'In dgViewClients.Rows
    Dim colStr As Array
    Dim colName As String
    'Dim findStr As Array

    colStr = {"Report_Type", "Client_Number", "Client_Name", "Client_Type", "Transmission_Type", "Local_Network", "TOA", "Contact_Person", "Phone_Number", "EMail_Address"}

    dgViewClients.SelectionMode = DataGridViewSelectionMode.FullRowSelect

    Found = False

    Try

        If dgViewClients.Rows.Count = 0 Then
            curRowInt = 0
        Else
            curRowInt = dgViewClients.CurrentRow.Index + 1
        End If
        If curRowInt > dgViewClients.Rows.Count Then
            curRowInt = dgViewClients.Rows.Count - 1
        End If
        If dgViewClients.Rows.Count > 0 Then
            For Each row As DataGridViewRow In dgViewClients.Rows
                'For Each cell As DataGridViewCell In row.Cells
                For Each colName In colStr
                    strToSearch = row.Cells(colName).Value.ToString.Trim.ToUpper
                    If strToSearch.Contains(strSearch.ToUpper) Then
                        Dim mCurrCell As DataGridViewCell =
                            row.Cells(colName)
                        dgViewClients.CurrentCell = mCurrCell
                        Found = True
                        If Found Then

                        End If
                    End If
                    'If Found Then
                    '    '    Exit For

                    'End If
                Next
            Next

        End If
        If Not Found Then
            MsgBox("Searched Value  " & txtSearch.Text & "  Not Found. Please try again.", MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
        End If
    Catch ex As Exception
        MsgBox("Error: " & ex.Message, MsgBoxStyle.Information Or MsgBoxStyle.OkOnly)
    End Try
    Return Found
End Function

and this is the code I am using for the textbox

Private Sub txtSearch_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearch.KeyDown


    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True

        findPart(txtSearch.Text)

    End If

End Sub

Recommended Answers

All 2 Replies

This is a very simplistic approach, see if it does what you want it to do.

For i = 0 To dgViewClients.Rows.Count - 1
   For j = 0 To dgViewClients.Rows(i).Cells.Count - 1
      If dgViewClients.Rows(i).Cells(j).Value.ToString.ToLower.Contains(strSearch.ToLower) Then
         dgViewClients.Rows(i).Selected = True
         Return True
      End If
   Next
Next

Return False

Is the DGV bound to a datatable?
Can you except the DGV showing only rows that match your search criteria?
If so, consider using setting the row filter on the datatable's DefaultView or creating a DataView and using it as the DGV's datasource.

commented: Very true +7
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.