C # Change the action of the enter in DataGridView

2

I want to change the default action so that the code does not skip the line, what I want to achieve is that when you touch the Enter select that row and return an ok.

if (e.KeyChar == Convert.ToChar(Keys.Enter))
{
    this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
    
asked by Daniel Aparicio 13.07.2018 в 09:40
source

1 answer

4

To prevent the event of pressing the enter from reaching DataGridView , what you must do is control the event KeyDown of DataGridView and put in it e.Handled=true; :

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        this.DialogResult = DialogResult.OK;
        e.Handled = true;
    }
}
    
answered by 13.07.2018 / 10:47
source