troubleshooting Question

Searching text in datagridview column in

Avatar of venkataramanaiahsr
venkataramanaiahsr asked on
Visual Basic.NET
3 Comments1 Solution2396 ViewsLast Modified:
I have a datagridview with list of items in the column. I have a text box where i type a search string.

Currently i have implemented a method which is working in almost all scenarios except the following one
  Suppose a list contains a 24hrs S Creatinine and S creatinine and when i type a  creatinine
   it searches first occurance of the string and highlights it.

My code for the same is as below.

Private Sub TextBox9_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox9.TextChanged


        dgvOIList.Sort(dgvOIList.Columns("Oiname"), System.ComponentModel.ListSortDirection.Ascending)
        Dim rowindex As Long = GridRowLocator(dgvOIList, "Oiname", TextBox9.Text.ToUpper, "Text")
        If rowindex = -1 Then Exit Sub

        dgvOIList.Rows(rowindex).Selected = True
        dgvOIList.FirstDisplayedScrollingRowIndex = rowindex

        dgvOIList.CurrentCell = dgvOIList.Item("Oiname", CInt(rowindex))
        dgvOIList.CurrentCell.Selected = True

    End Sub

Public Function GridRowLocator(ByVal dtg As DataGridView, ByVal ColumnName As String, _
                                         ByVal ValueToSearch As String, ByVal strSearchType As String) As Long

        Dim CurrentRowIndex As Long = -1

            Select Case strSearchType.ToUpper


                Case "TEXT"

                    If dtg.Rows.Count > 0 Then
                        For i = 0 To dtg.Rows.Count - 1
                  dtg.Rows(i).Cells(ColumnName).Value.ToString.Contains(ValueToSearch.ToUpper) = True Then
                                CurrentRowIndex = dtg.Rows(i).Index
                                Exit For
                            End If
                    End If

            End Select
        Catch ex As Exception
            MsgBox("Error: " & ex.Message, MsgBoxStyle.Information)
        End Try

        Return CurrentRowIndex

    End Function

My requirement is all the list items containing the search string should be grouped together one below another.  Can some expert pls advise on how to do this.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros