Link to home
Start Free TrialLog in
Avatar of Dangeriz
DangerizFlag for Taiwan, Province of China

asked on

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
Avatar of pivar
pivar
Flag of Sweden image

Hi,

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

/peter
Avatar of Jorge Paulino
If you press SHIFT+SPACE BAR will select the entire row
Avatar of abelallen
abelallen

Hello Dangeriz,

MSDN says something like this...

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


Regards,
Abel Allen
Avatar of Dangeriz

ASKER

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.
Hi Dangeriz,

I dont think its possible. Bcoz, u can only navigate row by row using the arrow keys. U cannot jump between rows.
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forgot to say: use F4 to start edit, then the arrow keys to move and the F5 to select
There's no known way to achieve what I need using keyboard only. But points will be awarded for this nice workaround. Thanks.
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