Hello Everybody,
I can Search in DataGridView using a textbox but It searches from Downwards
I want it to Search from Upwards
For Example I have these Values in the DataGridView
January
February
March
April
May
June
July
August
September
October
November
December
If I write "J" in textbox, January should be selected but it selects July
Here is my code
Textbox1 is the Textbox for Search and DGView is the DataGridView
Thanks in advance
I can Search in DataGridView using a textbox but It searches from Downwards
I want it to Search from Upwards
For Example I have these Values in the DataGridView
January
February
March
April
May
June
July
August
September
October
November
December
If I write "J" in textbox, January should be selected but it selects July
Here is my code
Textbox1 is the Textbox for Search and DGView is the DataGridView
Public Class Form1
Dim Flag As Boolean
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
dgview.ClearSelection()
For Each row As DataGridViewRow In dgview.Rows
For Each cell As DataGridViewCell In row.Cells
If cell.Value.StartsWith(TextBox1.Text, StringComparison.InvariantCultureIgnoreCase) Then
cell.Selected = True
dgview.CurrentCell = dgview.SelectedCells(0)
'Exit For
End If
Next
Next
dgview.Visible = True
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Up Then
DGView.Focus()
If Not DGView.CurrentRow.Index = 0 Then
If Not DGView.CurrentRow.Index = -1 Then
DGView.CurrentCell = DGView.Rows(DGView.CurrentRow.Index - 1).Cells(0)
Else
DGView.CurrentCell = DGView.Rows(DGView.CurrentRow.Index).Cells(0)
End If
End If
ElseIf e.KeyCode = Keys.Down Then
dgview.Focus()
If Not dgview.CurrentRow.Index = dgview.Rows.Count - 1 Then
If Not dgview.CurrentRow.Index = -1 Then
dgview.CurrentCell = dgview.Rows(dgview.CurrentRow.Index + 1).Cells(0)
Else
dgview.CurrentCell = dgview.Rows(dgview.CurrentRow.Index).Cells(0)
End If
End If
End If
If e.KeyCode = Keys.Return Then
If TextBox1.Text <> dgview.SelectedCells(0).Value Then
TextBox1.Text = dgview.SelectedCells(0).Value
e.SuppressKeyPress = True
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dgview.Rows.Add()
dgview.Rows.Add()
dgview.Rows.Add()
dgview.Rows.Add()
dgview.Item(0, 0).Value = "January"
dgview.Item(1, 0).Value = "February"
dgview.Item(0, 1).Value = "March"
dgview.Item(1, 1).Value = "April"
dgview.Item(0, 2).Value = "May"
dgview.Item(1, 2).Value = "June"
dgview.Item(0, 3).Value = "July"
dgview.Item(1, 3).Value = "August"
dgview.Item(0, 4).Value = "September"
dgview.Item(1, 4).Value = "October"
dgview.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
For j As Integer = 0 To dgview.Rows.Count - 1
If e.KeyChar <> Chr(Keys.Back) Then
If dgview.Rows(j).Cells(0).Value.ToString.StartsWith(TextBox1.Text & e.KeyChar, StringComparison.CurrentCultureIgnoreCase) Then
Flag = True
Exit For
Else
Flag = False
End If
Else
If TextBox1.Text.Length <> 0 Then
Flag = True
End If
End If
Next
If Flag = False Then
'If e.KeyChar <> Chr(Keys.Up) Then
If e.KeyChar <> Chr(Keys.Return) Then
If TextBox1.SelectedText = "" Then
e.Handled = True
Beep()
Else
Dim searchindex As Integer = 0
For Each row As DataGridViewRow In dgview.Rows
For Each cell As DataGridViewCell In row.Cells
If cell.Value.StartsWith(e.KeyChar, StringComparison.InvariantCultureIgnoreCase) Then
cell.Selected = True
MsgBox("Found")
dgview.CurrentCell = dgview.SelectedCells(0)
'Exit For
cell.Style.BackColor = Color.Yellow
searchindex += 1
End If
Next
Next
End If
End If
End If
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
If dgview.Focused = False Then
If TextBox1.Text = "" Then
If dgview.Rows.Count > 0 Then
dgview.Rows(0).Selected = True
TextBox1.Text = dgview.SelectedRows(0).Cells(0).Value
End If
End If
End If
If dgview.Focused = False Then
dgview.Visible = False
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If Flag = True Then
dgview.ClearSelection()
For Each row As DataGridViewRow In dgview.Rows
For Each cell As DataGridViewCell In row.Cells
If cell.Value.StartsWith(TextBox1.Text, StringComparison.InvariantCultureIgnoreCase) Then
cell.Selected = True
dgview.CurrentCell = dgview.SelectedCells(0)
'Exit For
End If
Next
Next
Else
If TextBox1.Text = "" Then
dgview.Rows(0).Selected = True
End If
End If
End Sub
End Class
Thanks in advance