How to handle KeyEvents in a DataGridViewCell?

Is there a Keydown event for a DataGridViewCell ?
What I'm trying to do is when the user enters a specific cell, he can press F1 to help this particular column. And some form will appear ...

What is this event?

+7
c # winforms datagridview
source share
5 answers

I found this code in the 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"); } } 
+15
source share

DataGridViewCell does not have any events, but you can listen to the KeyDown on the DataGridView itself, and then see which cell is selected:

 public void dataGridView_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F1) { var selectedCell = dataGridView.SelectedCells[0]; // do something with selectedCell... } } 
+4
source share

When a user types into a cell, he actually introduces a control that fits inside the cell for editing purposes. For example, a string column type will actually create a TextBox for use inside a cell for user input. Therefore, you need to actually connect to the KeyDown event in the TextBox, which is placed inside the cell when editing.

+3
source share

another solution

 private void grdDetalle_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { // Sólo queremos esta funcionalidad para determinadas columnas Clave y Nombre if ((grdDetalle.Columns[grdDetalle.CurrentCell.ColumnIndex].Name == "colClaveArticulo") || (grdDetalle.Columns[grdDetalle.CurrentCell.ColumnIndex].Name == "colNombre")) { /// Workarround para que estando editando en las columnas del grid Clave y Nombre /// podamos detectar cuando se dio F4 para lanzar el dialogo de busqueda del /// articulo. e.Control.KeyDown += new KeyEventHandler(dataGridViewTextBox_KeyDown); e.Control.Leave += new EventHandler(dataGridViewTextBox_Leave); } } private void dataGridViewTextBox_Leave(object sender, EventArgs e) { if ((grdDetalle.Columns[grdDetalle.CurrentCell.ColumnIndex].Name == "colClaveArticulo") || (grdDetalle.Columns[grdDetalle.CurrentCell.ColumnIndex].Name == "colNombre")) { try { (sender as DataGridViewTextBoxEditingControl).KeyDown -= new KeyEventHandler(dataGridViewTextBox_KeyDown); } catch (Exception ex) { } } } private void dataGridViewTextBox_KeyDown(object sender, KeyEventArgs e) { // F4 Pressed if ((grdDetalle.Columns[grdDetalle.CurrentCell.ColumnIndex].Name == "colClaveArticulo") || (grdDetalle.Columns[grdDetalle.CurrentCell.ColumnIndex].Name == "colNombre")) { if (e.KeyCode == Keys.F4) // 115 { MessageBox.Show("Oprimieron F4"); e.Handled = true; e.SuppressKeyPress = true; } } } 
0
source share

I know this is an old question, but I believe that I have improved the answer to the upper voice.

  IDataGridViewEditingControl _iDataGridViewEditingControl; private void SlotTimesDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (_iDataGridViewEditingControl is DataGridViewComboBoxEditingControl) { DataGridViewComboBoxEditingControl iDataGridViewEditingControl = _iDataGridViewEditingControl as DataGridViewComboBoxEditingControl; iDataGridViewEditingControl.KeyPress -= SlotTimesDGV_EditingControlShowing_KeyPress; } if (e.Control is DataGridViewComboBoxEditingControl) { DataGridViewComboBoxEditingControl iDataGridViewEditingControl = e.Control as DataGridViewComboBoxEditingControl; iDataGridViewEditingControl.KeyPress += SlotTimesDGV_EditingControlShowing_KeyPress; _iDataGridViewEditingControl = iDataGridViewEditingControl; } } private void SlotTimesDGV_EditingControlShowing_KeyPress(object sender, KeyPressEventArgs e) { MessageBox.Show(""); } 

By having an IDataGridViewEditingControl instance variable, you can remove the KeyPress event that will result in duplicate calls when moving through cells, and your event is not limited to just one cell type.

0
source share

All Articles