Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Catching a keypress in a DataGrid?

Posted on 2005-05-12
Medium Priority
Last Modified: 2012-06-27

I'd like to catch the KeyPress or the TextChanged event of a datagrid - it doesn't really matter which.

However, I've tried just about everything imaginable and it still doesn't work.  I've registered the form's KeyPress event, the DataGrid's KeyPress event, and the KeyPress events of individual TextBoxes in the TextBoxColumns.

The closest I can get is, when I have all of these event handlers registered, to get the handler to fire after I edit text in a DataGrid cell, then move to another cell and edit its text too.  Is there any way I can catch the very first KeyPress?  I have tried to call Focus() on the DataGrid, TextBoxColumns, etc. but it doesn't help.
Question by:yizchaknaveh

Accepted Solution

rubixxcube earned 2000 total points
ID: 13990108
I was able to accomplish by hooking into the TextBox Keypress event for the TextBoxColumn.
Drop a datagrid on a new form and add the following code:
private void Form1_Load(object sender, System.EventArgs e)
                  DataTable dt=new DataTable();
                  DataGridTableStyle style=new DataGridTableStyle();
                  for(int i=0; i<3;i++)
                        dt.Columns.Add("col " + i);

                        DataGridTextBoxColumn col=new DataGridTextBoxColumn();
                        col.HeaderText="test " + i;
                        col.MappingName="col " + i;
                        col.TextBox.KeyPress+=new KeyPressEventHandler(TextBox_KeyPress);

                  for(int i=0; i<5;i++)
                        dt.Rows.Add(new object[]{"Item " + i, "Stuff " + i, "Junk " + i});



            private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
                  Console.WriteLine("Key pressed " + e.KeyChar);

As soon as the grid loads a cell has focus, every key i type is printed out to the console.  

IMPORTANT NOTE: Keypresses will not fire for special keys that are already defined with default functionality in the datagrid such as arrow keys, tab key or enter key.

Let me know if this is not what you are looking for.



Author Comment

ID: 13991354
Excellent!  Thank you very much!  Works like a charm.

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question