DataGridView keydown event not working in C#
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#
Why KeyDown event is not working with datagridview c#?
The KeyDown event fires before the selection actually has changed.
Key events occur in the following order:
KeyDown
KeyPress
KeyUp
So a simple solution is to move your code to the KeyUp
event, which happens after the new row selection has happened.
How to handle KeyEvents in a DataGridViewCell?
I found this code in a forum, and it works.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Control;
tb.KeyPress += new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
e.Control.KeyPress += new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
}
private void dataGridViewTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
//when i press enter,bellow code never run?
if (e.KeyChar==(char)Keys.Enter)
{
MessageBox.Show("You press Enter");
}
}
DataGridView KeyDown manipulation (enter, left, right, etc.) not working when multiselect
In your GridNavigation
method, under both conditions, if (keys == Keys.Enter || keys == Keys.Right)
and else if (keys == Keys.Left)
, the guilty party here is the recurring line:
dataGridView1.Rows[iRow].Cells[i].Selected = true;
What went wrong?
When dataGridView1.MultiSelect == false
the above line effectively sets the current cell, not just the Selected
property. I.E:
CurrentCell = Rows[0].Cells[0]
,Rows[0].Cells[0].Selected = true
- User clicks
->
or EnterRows[0].Cells[1].Selected = true
CurrentCell = Rows[0].Cells[1]
Rows[0].Cells[0].Selected = false
However, when dataGridView1.MultiSelect == true
the same line of code does not set the current cell. So the current cell remains selected as well as the adjoining cell to the left or right respectively. I.E:
CurrentCell = Rows[0].Cells[0]
,Rows[0].Cells[0].Selected = true
- User clicks
->
or EnterRows[0].Cells[1].Selected = true
CurrentCell = Rows[0].Cells[0]
Rows[0].Cells[0].Selected = true
- User again clicks
->
or Enter- Expected results:
Rows[0].Cells[2].Selected = true
. - Actual results:
Rows[0].Cells[1].Selected = true
...again.
- Expected results:
Solution
If you want it to behave the same as MultiSelect = false
and only have one selected cell when you navigate left or right, just replace the guilty lines of code with:
dataGridView1.CurrentCell = dataGridView.Rows[iRow].Cells[i];
If you want to retain the previously selected cells, replace the guilty lines of code with:
DataGridViewSelectedCellCollection cells = dataGridView1.SelectedCells;
dataGridView1.CurrentCell = dataGridView1.Rows[iRow].Cells[i];
if (dataGridView1.MultiSelect)
{
foreach (DataGridViewCell cell in cells)
{
cell.Selected = true;
}
}
up and down key not firing keypress event textbox in datagridview cell
Try to use DataGridView.CellEnter
event:
Occurs when the current cell changes in the
DataGridView
control or when the control receives input focus.
or DataGridView.CellLeave
event
Occurs when a cell loses input focus and is no longer the current cell.
datagridview1_keypress does not working
The issue is related to the fact that once you start editing a cell's value, you're no longer within the DataGridView
but within the "Editing Control" defined by the cell's owning column. For a DataGridViewTextBoxColumn
this is a TextBox
. You'll need to hook into the events of the editing control. The details of which are shared in another answer:
Related Topics
How to Enumerate Through a Jobject
How to Seed an Admin User in Ef Core 2.1.0
How to Use Webbrowser Control Documentcompleted Event in C#
Asp .Net MVC Disable Client Side Validation at Per-Field Level
Convert from SQLdatareader to JSON
Why Do I Need a Memory Barrier
How to Disable Copy, Paste and Delete Features on a Textbox Using C#
Possible to Iterate Backwards Through a Foreach
Struct Constructor: "Fields Must Be Fully Assigned Before Control Is Returned to the Caller."
Limiting Double to 3 Decimal Places
Generating Xml File Using Xsd File
.Net - Windowstyle = Hidden VS. Createnowindow = True