Trapping Enter key in dataGrid cell

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
 
D

Dmitriy Lapshin [C# / .NET MVP]

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top