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

- 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:
- What is the reason for the inconsistent behaviour of the Enter key when the DataGridView has focus?
- How can I cause Enter to close the form with a DialogResult of OK when the DataGridView has focus?
I imagine that enter is a valid key for data-entry, along with tab, and one that they want to preserve for those users who are most used to the keyboard, as opposed to point and click.
Have you tried adding a call to PerformClick(), perhaps within your key-down event handler?