yonan2236
yonan2236

Reputation: 13649

DataGridView KeyDown Event not working

I have a DataGridView with a KeyDown event.

void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.C && e.Modifiers == (Keys.Control | Keys.Shift)) 
    {     
        MessageBox.Show("ok"); // to test if working
    }
}

The message box prompts when I press the key combinations.
But that is only when the datagridview is empty (no rows of data). The code doesn't work when the datagridview has rows already.

Is there any wrong with the codes?

Upvotes: 2

Views: 4219

Answers (3)

pstrjds
pstrjds

Reputation: 17428

I believe you are going to have to derive from the DataGridView class in order to get the key processing you want. Another alternative would be to add a message filter, but I would go with the derived data grid as my first approach.

Upvotes: 0

user596075
user596075

Reputation:

The KeyDown event handler doesn't fire when the DataGridView is in edit mode.

Upvotes: 3

Dan Iveson
Dan Iveson

Reputation: 936

The datagridview has child controls with their own events. I think you need to catch the row and/or cell keydown events too.

Upvotes: 0

Related Questions