DataGridView keydown event not working in C#
Solution 1
Whenever a cell is in edit mode, its hosted control is receiving the KeyDown
event instead of the parent DataGridView
that contains it. That's why your keyboard shortcut is working whenever a cell is not in edit mode (even if it is selected), because your DataGridView
control itself receives the KeyDown
event. However, when you are in edit mode, the edit control contained by the cell is receiving the event, and nothing happens because it doesn't have your custom handler routine attached to it.
I have spent way too much time tweaking the standard DataGridView
control to handle edit commits the way I want it to, and I found that the easiest way to get around this phenomenon is by subclassing the existing DataGridView
control and overriding its ProcessCmdKey
function. Whatever custom code that you put in here will run whenever a key is pressed on top of the DataGridView
, regardless of whether or not it is in edit mode.
For example, you could do something like this:
class MyDataGridView : System.Windows.Forms.DataGridView
{
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
MessageBox.Show("Key Press Detected");
if ((keyData == (Keys.Alt | Keys.S)))
{
//Save data
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Also see related, though somewhat older, article: How to trap keystrokes in controls by using Visual C#
Solution 2
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
}
}
Solution 3
This is true that EditingControlShowing can help, but not if you wants to catch the Enter key. In that case, one should use the following method:
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewTextBoxEditingControl)
{
DataGridViewTextBoxEditingControl tb = e.Control as DataGridViewTextBoxEditingControl;
tb.KeyDown -= dataGridView_KeyDown;
tb.PreviewKeyDown -= dataGridView_PreviewKeyDown;
tb.KeyDown += dataGridView_KeyDown;
tb.PreviewKeyDown += dataGridView_PreviewKeyDown;
}
}
void dataGridView_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
<your logic goes here>
}
}
Solution 4
A simpler way I just tried out is as follows:
- Set the
KeyPreview
property of the Form totrue
. - Instead of catching the KeyDown event on Grid, catch the KeyDown event on Form.
Code as follows:
Private Sub form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If grd.Focused Then
'Do your work
End If
End Sub
Comments
-
Javed Akram about 3 years
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 is not in edit mode, but if it is in edit mode below code is not working
private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == (Keys.Alt | Keys.S)) { //save data } }
-
user1703401 over 13 yearsGood advice. The WM_KEYDOWN test isn't necessary btw.
-
Javed Akram over 13 yearsThank you very much, for your advice
-
Cody Gray over 13 years@Hans Passant: I agree that it's not necessary, and I normally never include it. I went back to add it after I saw that the article I linked to recommended it as a "good practice."
-
user1703401 over 13 yearsTypical KB verbiage. You won't know its BS until you put it to the test. 0% hit so far.
-
Larry about 10 years+1: This is the best way for me as I dont want to subclass my DataGridView.
-
causa prima about 8 yearsVery simple, especially as my entire form is the DataGridView.
-
barlop about 8 yearssuppose you have a form, with a)the subclass of datagridview and b)a textbox. And you want the textbox to have the value that is in the edited cell after the key has been pushed down.. Any idea how one would one do that?
-
Robert Koernke over 6 yearsI was not clear on why '-=', I also converted the above to VB.net:
private sub dgvGrid1_editing(sender as Object, e as DataGridViewEditingControlShowingEventArgs) Handles dgvGrid1.EditingControlShowing if Typeof e.Control is DataGridViewTextBoxEditingControl Then Dim tb as DataGridViewTextBoxEditingControl = e.Control AddHandler tb.Keydown, AddressOf dgvGrid1_keydown End if End Sub
-
user3147973 over 4 yearsThis doesn't work with alphanumeric keys. It still enters the editing control.
-
kuklei almost 3 yearsThanks for the suggestion. I my case I wanted to compare the cell value before the DataGridView.EndEdit and after, but the enter key was committing the value. This way I could not compare. in "<your logic goes here>" I moved focus to another control and used the Cell_Leave event to calculate the changed values