Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

key press to cycle through rows starting with same letter 1

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
I've got a DataGridView and I want to cycle through each row that begins with the letter key that is pressed.

In other words, if I have a list like this:
Alpha
Baker
Beta
Bravo
Charlie
Delta
The first time I pres the "B" key, the "Baker" row is selected. If I hit the "B" key again, the "Beta" row is selected. If the current row is "Bravo", pressing "B" will take me back up to the "Baker" row.

So far, I've only figured out how to jump to the first row using this code:
Code:
    Private Sub JumpToRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgvServiceList.KeyPress
        strKeyChar = UCase(e.KeyChar)
        If Char.IsLetter(strKeyChar) Then
            For Each row As DataGridViewRow In dgvServiceList.Rows
                If UCase(row.Cells(0).Value.ToString.StartsWith(strKeyChar)) Then
                    dgvServiceList.CurrentCell = dgvServiceList.Item(0, row.Index)
                    Exit For
                End If
            Next
        End If
    End Sub

Any have an idea as to how I can jump to the next item on each key press?
 

I would do it like this:

Code:
Dim StartIndex As Integer
Dim bFound As Boolean = False

StartIndex = dgvServiceList.CurrentRow.Index

For i As Integer = StartIndex to dgvServiceList.Rows.Count - 1
    If UCase(dgvServiceList.Rows(i).Cells(0).Value.ToString.StartsWith(strKeyChar)) Then
        dgvServiceList.CurrentCell = dgvServiceList.Item(0, i)
        bFound = True
        Exit For
    End If
Next

If Not bFound Then
    'Char not found, so loop back to starting point
    For i As Integer = 0 to StartIndex - 1
        If UCase(dgvServiceList.Rows(i).Cells(0).Value.ToString.StartsWith(strKeyChar)) Then
            dgvServiceList.CurrentCell = dgvServiceList.Item(0, i)
            bFound = True
            Exit For
        End If
    Next
End If

If Not bFound Then
    MsgBox(strKeyChar & " not found."
End If

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I gave your code a try and it won't cycle through the lines that begin with the same letter. It just jumps to the first entry like my code did.
 
Ok, I just typed that code in without testing. The problem is that you need to store the index of the last row found, so the code won't just keep hitting the same row, as it is now. This can be accomplished with a static variable:

Dim StartIndex As Integer
Dim bFound As Boolean = False
Dim strKeyChar As String
[red]Static LastRow As Integer[/red]

strKeyChar = UCase(e.KeyChar)

StartIndex = dgv1.CurrentRow.Index

For i As Integer = StartIndex To dgv1.Rows.Count - 1
If i <> LastRow Then
If UCase(dgv1.Rows(i).Cells(0).Value.ToString.StartsWith(strKeyChar)) Then
dgv1.CurrentCell = dgv1.Item(0, i)
bFound = True
[red]LastRow = i[/red]
Exit For
End If
End If
Next

If Not bFound Then
'Char not found, so loop back to starting point
For i As Integer = 0 To StartIndex - 1
If i <> LastRow Then
If UCase(dgv1.Rows(i).Cells(0).Value.ToString.StartsWith(strKeyChar)) Then
dgv1.CurrentCell = dgv1.Item(0, i)
bFound = True
[red]LastRow = i[/red]
Exit For
End If
End If
Next
End If

If Not bFound Then
MsgBox(strKeyChar & " not found.")
End If

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks, jebenson. That fixed it. I can now cycle through the rows like I wanted. You rock.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top