WinForms DataGridView MultiSelect with Keyboard Only

Hi All,

My customer has a request to have access to all functionality in their Windows Applications with a keyboard only.

Is there a way to MultiSelect a row (or cell) in a DataGridView with a keyboard only? I know holding down the shift key while moving the arrow keys will select multiple cells/rows in a block. But what if the the selection required is not in a block?

Thanks.
Dave
LVL 4
DangerizAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pivarCommented:
Hi,

Have you tried holding down CTRL instead of SHIFT and select the rows? That works normally in windows.

/peter
0
Jorge PaulinoIT Pro/DeveloperCommented:
If you press SHIFT+SPACE BAR will select the entire row
0
abelallenCommented:
Hello Dangeriz,

MSDN says something like this...

http://msdn.microsoft.com/en-us/library/tb9t9a2t.aspx


Regards,
Abel Allen
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

DangerizAuthor Commented:
I've tried holding the CTRL down, but that only works with mouse clicks.

I know about the MSDN link, I've tried all the options (and also saw the SHIFT + SPACE to select the whole row) but none of them show how to multiselect inconsecutively using keyboard only.
0
rajeeshmcaCommented:
Hi Dangeriz,

I dont think its possible. Bcoz, u can only navigate row by row using the arrow keys. U cannot jump between rows.
0
Jorge PaulinoIT Pro/DeveloperCommented:
That was funny ... I didn't found an elegant solution for this, but you can try with this walkaround.
I thinks it's more or less what you need.

    Private row, col As Integer
    Private editMode As Boolean

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.F4
                editMode = Not editMode
            Case Keys.F5
                If editMode Then
                    Me.DataGridView1(col, row).Selected = True
                End If
            Case Keys.Up
                If editMode Then
                    row -= 1
                    If row < 0 Then row = 0
                    e.SuppressKeyPress = True
                End If
            Case Keys.Down
                If editMode Then
                    row += 1
                    If row > Me.DataGridView1.RowCount - 1 Then row = Me.DataGridView1.RowCount - 1
                    e.SuppressKeyPress = True
                End If
            Case Keys.Left
                If editMode Then
                    col -= 1
                    If col < 0 Then col = 0
                    e.SuppressKeyPress = True
                End If
            Case Keys.Right
                If editMode Then
                    col += 1
                    If col > Me.DataGridView1.ColumnCount - 1 Then col = Me.DataGridView1.ColumnCount - 1
                    e.SuppressKeyPress = True
                End If
        End Select

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
    End Sub

    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        row = Me.DataGridView1.CurrentCell.RowIndex
        col = Me.DataGridView1.CurrentCell.ColumnIndex
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jorge PaulinoIT Pro/DeveloperCommented:
Forgot to say: use F4 to start edit, then the arrow keys to move and the F5 to select
0
DangerizAuthor Commented:
There's no known way to achieve what I need using keyboard only. But points will be awarded for this nice workaround. Thanks.
0
DangerizAuthor Commented:
Just added some extra functionality to jpaulino's code to highlight the current cell position. The code is not a solution to any problem, it's just a sample for my customer to prove that it is possible with code.
Private row, col As Integer
    Private editMode As Boolean

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.F4
                editMode = Not editMode
                If editMode Then
                    lblEditMode.Text = "DataGridView Edit Mode"
                    lblEditMode.ForeColor = Color.Red
                Else
                    lblEditMode.Text = ""
                    Me.DataGridView1(col, row).Style.BackColor = Color.White
                End If

            Case Keys.F5
                If editMode Then

                    Me.DataGridView1(col, row).Selected = Not Me.DataGridView1(col, row).Selected
                End If
            Case Keys.Up
                If editMode Then
                    Me.DataGridView1(col, row).Style.BackColor = Color.White
                    row -= 1
                    If row < 0 Then row = 0
                    Me.DataGridView1(col, row).Style.BackColor = Color.Yellow
                    e.SuppressKeyPress = True
                End If
            Case Keys.Down
                If editMode Then
                    Me.DataGridView1(col, row).Style.BackColor = Color.White
                    row += 1
                    If row > Me.DataGridView1.RowCount - 1 Then row = Me.DataGridView1.RowCount - 1
                    Me.DataGridView1(col, row).Style.BackColor = Color.Yellow
                    e.SuppressKeyPress = True
                End If
            Case Keys.Left
                If editMode Then
                    Me.DataGridView1(col, row).Style.BackColor = Color.White
                    col -= 1
                    If col < 0 Then col = 0
                    Me.DataGridView1(col, row).Style.BackColor = Color.Yellow
                    e.SuppressKeyPress = True
                End If
            Case Keys.Right
                If editMode Then
                    Me.DataGridView1(col, row).Style.BackColor = Color.White
                    col += 1
                    If col > Me.DataGridView1.ColumnCount - 1 Then col = Me.DataGridView1.ColumnCount - 1
                    Me.DataGridView1(col, row).Style.BackColor = Color.Yellow
                    e.SuppressKeyPress = True
                End If
        End Select

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
    End Sub

    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        row = Me.DataGridView1.CurrentCell.RowIndex
        col = Me.DataGridView1.CurrentCell.ColumnIndex
    End Sub

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.