Avatar of PeterBaileyUk
PeterBaileyUk
 asked on

search code in vb.net datagrid

I have some code That searches a listbox from text enetered in a textbox and would like to create  the same but using data grid instead if it is possible, i posted this code so the functionality can be seen, i suspect the datagrid code would be entirely different.

The code I have provided by one of the experts but the question id is unknown is the following. ive amended the textbox search name and added the datagrid name in the code but the method to do this must be slightly different. I want to type something into the search box and once entered it finds the text in the grid based on column

   Private Sub TextSearchStrDescCW_KeyUp(sender As Object, e As KeyEventArgs) Handles TextSearchStrDescCW.KeyUp
        Dim index = -1
        If TypeOf sender Is TextBox Then
            Dim tb = DirectCast(sender, TextBox)
            If tb.Equals(TextSearchStrDescCW) Then
                If e.KeyCode.Equals(Keys.Enter) Then
                    index = DataGridViewStringsCW.FindString(tb.Text)  'underlined here
                    If index <> -1 Then
                        DataGridViewStringsCW.SetSelected(index, True) 'underlined here
                    End If
                    tb.Clear()
                    e.Handled = True
                End If
            End If
        ElseIf TypeOf sender Is ListBox Then
            Dim lb = DirectCast(sender, ListBox)
            If lb.Equals(DataGridViewStringsCW) Then
                If e.KeyCode.Equals(Keys.Enter) Then
                    index = lb.FindString(TextSearchStrDescCW.Text)
                    If index <> -1 Then
                        lb.SetSelected(index, True)
                    End If
                    TextSearchStrDescCW.Clear()
                    e.Handled = True
                ElseIf e.KeyCode.Equals(Keys.Back) Then
                    If TextSearchStrDescCW.Text.Length > 0 Then
                        TextSearchStrDescCW.Text = TextSearchStrDescCW.Text.Substring(0, TextSearchStrDescCW.Text.Length - 1)
                    End If
                Else
                    TextSearchStrDescCW.Text = TextSearchStrDescCW.Text + Convert.ToChar(e.KeyCode)
                End If
            End If
        End If
    End Sub

Open in new window

Visual Basic.NET

Avatar of undefined
Last Comment
PeterBaileyUk

8/22/2022 - Mon
PeterBaileyUk

ASKER
I tried this approach but no joy with this

    Private Sub TextSearchStrDescCW_KeyUp(sender As Object, e As KeyEventArgs) Handles TextSearchStrDescCW.KeyUp
        DataGridViewStringsCW.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        Dim intcount As Integer = 0
        For Each Row As DataGridViewRow In DataGridViewStringsCW.Rows
            If DataGridViewStringsCW.Rows(intcount).Cells(0).Value.ToString = TextSearchStrDescCW.Text.ToString() Then
                DataGridViewStringsCW.Rows(intcount).Selected = True


            End If
            intcount += 1
        Next Row

    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
PeterBaileyUk

ASKER
For some reason my final comment didnt show..thank you so much the presentation stage by stage really aids my learning.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes