Capturing Control Keys (Arrow Movement) within the DataGrid

Posted on 2004-11-27
Last Modified: 2008-02-01
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!
Question by:bmccleary
    LVL 4

    Accepted 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

    Author Comment

    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.
    LVL 4

    Expert Comment

      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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now