DataGridView keydown event not working in C#

前端 未结 5 669
北恋
北恋 2020-11-28 11:02

DataGridView keydown event is not working when I am editing text inside a cell.

I am assigning shortcut Alt+S to save the data, it works when cell

5条回答
  •  攒了一身酷
    2020-11-28 11:17

    Another way of doing it is by using the EditingControlShowing event to redirect the event handling to a custom event handler as below:

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
     {
        if (e.Control is DataGridViewTextBoxEditingControl tb)
                {
                    tb.KeyDown -= dataGridView1_KeyDown;
                    tb.KeyDown += dataGridView1_KeyDown;
                }
     }
    
    //then in your keydown event handler, execute your code
    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
     {
        if (e.KeyData == (Keys.Alt | Keys.S))
        {
             //save data
        }
     }
    

提交回复
热议问题