'How do I get "Accept" button to fire when DataGridView has focus?

I am presenting a form as a dialog box. The form contains a DataGridView, a TextBox and OK/Cancel buttons, as follows:

enter image description here

  • I have set the AcceptButton property of the form to the OK button and the CancelButton property of the form to the Cancel button.
  • I have set the DialogResult property of the OK button to "OK" and the DialogResult property of the Cancel button to "Cancel"

If the textbox has focus, then pressing Enter closes the form with a DialogResult of OK. However, if the DataGridView has focus then pressing Enter does not close the form.

Pressing the Escape key, however, always results in the form closing with a DialogResult of Cancel.

This is a two part question:

  1. What is the reason for the inconsistent behaviour of the Enter key when the DataGridView has focus?
  2. How can I cause Enter to close the form with a DialogResult of OK when the DataGridView has focus?


Solution 1:[1]

The DataGridView uses the enter key to move to the cell below the cell currently being edited. There is no single property to change this behaviour, but you can override the keydown behaviour of the grid:

dataGridView1.KeyDown += new KeyEventHandler(dataGridView1_KeyDown);

void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        button1.PerformClick();
        e.Handled = true;
    }
}

This still leaves you with arrow keys for navigation and still allows users to add new rows (the new row appears as soon as data is entered in the bottom row of the grid).

Solution 2:[2]

  1. The DataGridView's cell handles the KeyDown event internally if the KeyCode is Enter (if the cell is in Edit mode, pressing Enter means "I've finished editing the cell". if an entire row is selecting, pressing Enter means "Add a new row").
  2. I'm assuming that you don't want to add a new row when the user presses Enter. First set the DataGridView.AllowUsersToAddNewRows property to false so that users don't get any unexpected behavior, and then Handle the DataGridView.KeyDown event and manually press the button when the event is raised with the KeyCode.Enter.

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.Enter)
        {
            button1.PerformClick();
        }
    }
    

Solution 3:[3]

I'm not sure I fully understand the question, but I'll give my best go. Let me know if I'm missing something.

The way you should do this is to add a keypress event to the Form for handling the Enter/Escape key presses.

This is for adding the keypress event in the code behind (can use designer).

this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);

The event handler could be something like this:

void SelectionPageForAutomation_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == Key.Escape)
        {
            btn_Cancel.PerformClick();
        }
        else if(e.KeyChar == Key.Enter){
            btn_Okay.PerformClick();
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 phoenix
Solution 2 scripni
Solution 3 ImGreg