Winform Datagridview Tab and Arrow Keys

I want to handle the KeyDown event in a DataGridView cell. I use the following code to get the KeyDown event in a cell:

private void dgvData_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { var tb = (DataGridViewTextBoxEditingControl)e.Control; tb.KeyDown += cell_KeyDown; } 

But it looks like I can't handle special keys like tab and arrows. These keys do not go to my cell_KeyDown method. Therefore, I am trying to process them in the KeyDown DataGridView event:

 private void dgvData_KeyDown(object sender, KeyEventArgs e) { // handle keys } 

In this case, I still cannot capture the Tab key. However, I can grab the arrow keys, after processing my custom events, it still moves to the other cells in the arrow. I want to stay in the cell.

Then I extend the DataGridView as follows:

 class DataGridViewSp : DataGridView { protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Tab) { //todo special handling return true; } else if (keyData == Keys.Down) { //todo special handling return true; } else if (keyData == Keys.Up) { //todo special handling return true; } else { return base.ProcessDialogKey(keyData); } } } 

Now I can grab the Tab key in this overridden ProcessDialogKey method. But, nevertheless, it does not capture the Down and Down arrow keys. Something is wrong?

The ideal solution would be in cell editing mode, it processes the tab and arrow keys in my path and remains in the cell. When in the grid arrows and tab keys work in the usual way.

+4
source share
1 answer

Instead of ProcessDialogKey use ProcessCmdKey . Then you grab all the keys you need.

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Tab) { //todo special handling return true; } return base.ProcessCmdKey(ref msg, keyData); } 
+3
source

All Articles