Trapping Enter key in dataGrid cell

  • Thread starter Thread starter Randy
  • Start date Start date
R

Randy

Hello All,
I'm trying to trap the enter key in my dataGrid using the event...
aColumnTextColumn.TextBox.KeyPress += new
KeyPressEventHandler(DateKeyPress);

The event fires when there is a key pressed for the cell except for the
Enter key. Apparently this key doesn't fire this event. My event code is...
private void DateKeyPress(object sender, KeyPressEventArgs e)
{
Console.WriteLine(e.KeyChar);
switch ((char)(e.KeyChar))
{
case '\r': //Enter key
e.Handled = true;
break;

default: // Everything else
e.Handled = false;
break;
}
}

When I modify a cell's value and press the Enter key, my break point on the
e.Handled = true; for case '\r': doesn't even hit. Can someone tell me what
I'm doing wrong?

Thanks
 
Hello Randy,

You can't trap the Enter keystroke this way. What you need is inheriting
from the grid control and overriding its methods such as ProcessDialogKey,
ProcessCmdKey and ProcessKeyPreview. I can't remember which of them is
exatcly responsible for the Enter key, you will have to experiment :-(

I can however assure you that it is possible to intercept practically every
keystroke to force the user to stay in a cell unless she enters a correct
value. Hopefully knowing it IS possible will give you enough motivation for
research :-)
 
Back
Top