Avatar of venkataramanaiahsr
venkataramanaiahsr asked on

Searching text in datagridview column in vb.net

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.
Visual Basic.NET

Avatar of undefined
Last Comment

8/22/2022 - Mon
Jerry Miller

Rather than build something yourself, have you looked at the Ajax Control Toolkit? The AutoComplete Extender does this very well.


Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

It is windows forms application. I could solve the said problem in another way by using bindingsource filter method.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy