Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Capturing Control Keys (Arrow Movement) within the DataGrid

I've seen a bunch of examples here on how to capture the KeyPress and KeyDown events within a datagrid to capture arrow movement within a datagrid, but there's been so many varying options, that I am now thouroughly confused.  I have a very simple requirement that I have spent hours trying to solve, to no avail.  The following is what I have/need:

I have a read only data grid that displays a list of items in 3-5 columns.  I am only using the data grid to list items so that when a user clicks a row, another action fires in my application.  In other words, no editing, adding or deleting... only clicking and sorting.  This works very well with the default datagrid options, but I need two things to happen which I can't seem to control.  The first, is that I don't want the user to "enter" a cell.  Instead, when they click a cell, the entire row is simply highlighted, and it does not appear as though the user can edit the text in the cell.  Even setting the datagrid to read-only doesn't prevent this.  The second requirement is that when the user clicks the right or left arrow keys, no action is taken within the datagrid (e.g. cell or row shifting), but a message box appears saying "You clicked the left arrow key" (obviously, there is more to it than this, but this should get me there).  Finally, I need the up/down arrow keys to function as they do now and select the previous and next records, but again, just highlighting the row, not entering the individual cell.  Can anyone provide me an example of this.   I would think it should be easy.  Any help is apprecitated!
  • 2
1 Solution
This may only help with the keypress part of your question, not the entering cell part of your question

  Private Sub DataGrid1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGrid1.KeyUp
        Select Case e.KeyCode
            Case Keys.Up : MsgBox("Up Arrow Pressed")
            Case Keys.Right : MsgBox("Right Arrow Pressed")
            Case Keys.Down : MsgBox("Down Arrow Pressed")
            Case Keys.Left : MsgBox("Left Arrow PRessed")
        End Select
    End Sub
bmcclearyAuthor Commented:
Thanks for the response, but unfortunately, the key up event for the data grid doesn't work, because the text box within the datagridcolumn is the control that raises the event, not the entire data grid.  I tried your exampe and the arrow key clicks are not captured or raised.  I was hoping it was going to be that easy, but unfortunately, it is not.
  This isn't exactly right, but it may help you get going...

Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
        If DataGrid1.CurrentCell.ColumnNumber = (iOldCol + 1) Then
            MsgBox("RightArrow Pressed")
            DataGrid1.CurrentCell = New DataGridCell(iOldCol - 1, iOldRow)
        End If
        If DataGrid1.CurrentCell.ColumnNumber = (iOldCol - 1) Then
            MsgBox("LeftArrow Pressed")
            DataGrid1.CurrentCell = New DataGridCell(iOldCol + 1, iOldRow)
        End If

        If DataGrid1.CurrentCell.RowNumber = (iOldRow + 1) Then
            MsgBox("DownArrow Pressed")
            DataGrid1.CurrentCell = New DataGridCell(iOldCol, iOldRow - 1)
        End If
        If DataGrid1.CurrentCell.RowNumber = (iOldRow - 1) Then
            MsgBox("UpArrow Pressed")
            DataGrid1.CurrentCell = New DataGridCell(iOldCol, iOldRow + 1)
        End If

        iOldCol = DataGrid1.CurrentCell.ColumnNumber
        iOldRow = DataGrid1.CurrentCell.RowNumber
    End Sub

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now