Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

WinForms DataGridView MultiSelect with Keyboard Only

Posted on 2010-01-13
9
Medium Priority
?
1,712 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
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

577 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