Search code examples
c#winformsdatagridviewdatagridviewcombobox

Is it possible to start editing a DataGridViewComboBoxCell by pressing down key?


I'm working on a project where I use a DataGridView, with some of the columns being DataGridViewComboBoxColumn.
My users want to edit these ComboBoxCell by pressing Down key.

I've actually two ideas on how doing it:

  1. Change the key that starts editing mode of these cells to Down key
  2. Check event Keydown on my DataGridView and if CurrentCell is a ComboBoxCell force the dropdown of this cell

But I didn't manage to find a way to do any of these.

So, Is there a way to achieve this? (Even if it doesn't use one of my ideas)


Solution

  • You can use DataGridView.KeyDown event:

    Private Sub DataGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    
        If e.KeyValue = Keys.Down Then
            With Me.DataGridView1
                If .Columns(.CurrentCell.ColumnIndex).GetType.Name = "DataGridViewComboBoxColumn" Then
                    If Not .IsCurrentCellInEditMode Then
                        .BeginEdit(True)
                        CType(.EditingControl, ComboBox).DroppedDown = True
                        e.Handled = True
                    End If
                End If
            End With
        End If
    
    End Sub
    

    Sorry, VB.NET code but you can easily translate it to C#.