troubleshooting Question

search code in vb.net datagrid

Avatar of PeterBaileyUk
PeterBaileyUk asked on
Visual Basic.NET
3 Comments1 Solution113 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
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