Catching a keypress in a DataGrid?

Posted on 2005-05-12
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
    LVL 2

    Accepted Solution

    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

    Excellent!  Thank you very much!  Works like a charm.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now