views:

40

answers:

3

Is there a Keydown Event of a DataGridViewCell?
What I'm trying to do is when a user is typing in a particular cell, he can press F1 for help of that particular column. And some Form will popup...

What event it is?

+2  A: 

DataGridViewCell doesn’t have any events, but you can listen for the KeyDown event on the DataGridView itself and then look at 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...
    }
}
Timwi
ok sir...i'll try. thanks
yonan2236
+2  A: 

When the user types into a cell it is actually typing into the control that is placed inside the cell for editing purposes. For example, a string column type will actually create a TextBox for use inside the cell for the user to input against. So you need to actually hook into the KeyDown event of the TextBox that is placed inside the cell when editing takes place.

Phil Wright
+1  A: 

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");
  }
}
yonan2236