?
Solved

WinForms DataGridView MultiSelect with Keyboard Only

Posted on 2010-01-13
9
Medium Priority
?
1,677 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:Dangeriz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 26301934
Hi,

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

/peter
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 26301935
If you press SHIFT+SPACE BAR will select the entire row
0
 
LVL 2

Expert Comment

by:abelallen
ID: 26302257
Hello Dangeriz,

MSDN says something like this...

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


Regards,
Abel Allen
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 4

Author Comment

by:Dangeriz
ID: 26302413
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
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26302688
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
 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 total points
ID: 26303504
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 26303512
Forgot to say: use F4 to start edit, then the arrow keys to move and the F5 to select
0
 
LVL 4

Author Closing Comment

by:Dangeriz
ID: 31676552
There's no known way to achieve what I need using keyboard only. But points will be awarded for this nice workaround. Thanks.
0
 
LVL 4

Author Comment

by:Dangeriz
ID: 26415120
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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