473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Need help with DataGrid Navigation / Cancelling Keystroke in PreviewKeyDown Event

I'm noticing that when my DataGrid has focus, it handles the Enter key by
advancing to the next row in the grid. This neutralizes Forms's current
defined AcceptButton.

My primary question: is there any quick way to configure the Grid not to
handle the Enter key? I do not see any property control which might
configure this behavior.

My secondary question: If there's no quick solution via Grid configuration,
then how do I get the following workaround to work? I've implemented the
PreviewKeyDown event to trap for the Enter key. This is working, but only
partially. Using this approach, the AcceptButton is triggered, however I
need to then suppress the Enter key so that the Grid doesn't handle it.
However, I don't see anything in the PreviewKeyDownEventArgs object to allow
me to cancel the keystroke.

private void gridResults_PreviewKeyDown(object sender,
PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.AcceptButton.PerformClick();
}
}

Thanks for any help which you can provide.

- Joseph Geretz -
Mar 15 '07 #1
1 5252
OK, I was able to implement this successfully using the KeyDown event,
rather than the PreviewKeyDown event.

private void gridResults_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.AcceptButton.PerformClick();
e.Handled = true;
}
}

Kind of leaves me wondering though, what's the point in the PreviewKeyDown
event if you can't cancel the key? What would be a scenario where you'd need
to implement KeyDownPreview as opposed to KeyDown?

Thanks,

- Joe Geretz -

"Joseph Geretz" <jg*****@nospam.comwrote in message
news:uV**************@TK2MSFTNGP03.phx.gbl...
I'm noticing that when my DataGrid has focus, it handles the Enter key by
advancing to the next row in the grid. This neutralizes Forms's current
defined AcceptButton.

My primary question: is there any quick way to configure the Grid not to
handle the Enter key? I do not see any property control which might
configure this behavior.

My secondary question: If there's no quick solution via Grid
configuration, then how do I get the following workaround to work? I've
implemented the PreviewKeyDown event to trap for the Enter key. This is
working, but only partially. Using this approach, the AcceptButton is
triggered, however I need to then suppress the Enter key so that the Grid
doesn't handle it. However, I don't see anything in the
PreviewKeyDownEventArgs object to allow me to cancel the keystroke.

private void gridResults_PreviewKeyDown(object sender,
PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.AcceptButton.PerformClick();
}
}

Thanks for any help which you can provide.

- Joseph Geretz -

Mar 15 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul THompson | last post by:
I have been working for some time to 1) detect tab and shift tab events 2) control the focus on the basis of these events. I have found that I can do this, but continue to have nagging problems. ...
8
by: InvisibleDuncan | last post by:
I have a ListView that populates some fields whenever the user selects an item. However, if they change the data in the fields and then select a new item without saving, I want to display a message...
7
by: DraguVaso | last post by:
Hi, I have a Datagrid, and I want to limit the Lenght of the text in a certain column to a certain number of characters (for exemple 10). What I want is that, when the user is typing in the...
2
by: Greg T | last post by:
Hi, I have a rather long form that I don't want people submitting unless they are absolutely sure they are ready. I figured the easiest way to prevent an accidental form submission by way of...
0
by: shravan | last post by:
Hi, I have a extended Windows Forms Datagrid, I have custom column styles defined for it. The problem is when I am in the new row and pressing escape two times - First time it's cancelling cell...
1
by: melanieab | last post by:
Hi again, I have a textbox where the text is longer than the textbox width. When I type something in there or even do any single keystroke (other than tab, up, down, or enter) and then leave, the...
18
by: len.hartley | last post by:
Hi, I am trying to pop-up a window when the user clicks on an image. The problem is that when the user clicks on the image and the window pops up OK, but the window underneath also proceeds to...
10
by: amiga500 | last post by:
Hello, I have one basic simple question. When I have multiple records in the datagrid as follows: Code Product 1 Product 2 Product 3 11111 A B C...
9
by: pvsundarram | last post by:
hey, i am trying to cancel the keydown event for certain keycodes( for eg:- enter key ).But the cancelling of this event is not happening in firefox. Is there any way to cancel the event in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.