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
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