DataGridView KeyDown Event not working

柔情痞子 提交于 2020-01-04 11:43:24

问题


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?


回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/9034368/datagridview-keydown-event-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!