Take the 2-minute tour ×

I'm developed an application for the datagridview filtering . And i used the datagridview's dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) event for filtering. But i want to handle it on the key press event for the datagridview cell. But i'm not getting that type of event.

the datagridview event should occure on the each keypress..

So can anybody tell me that which event should i use for the datagridview?

please help me... thanx

share|improve this question

1 Answer 1

up vote 3 down vote accepted

The DataGridView.KeyPress event will not be raised when the user types in a particular cell. If you want to be notified each time they press a key while editing content in a cell, you have two options:

  1. Handle the KeyPress event that is raised directly by the editing control itself (which you can access using the EditingControlShowing event).

    For example, you might use the following code:

    public class Form1 : Form
        public Form1()
            // Add a handler for the EditingControlShowing event
            myDGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(myDGV_EditingControlShowing);
        private void myDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            // Ensure that the editing control is a TextBox
            TextBox txt = e.Control as TextBox;
            if (txt != null)
                // Remove an existing event handler, if present, to avoid adding
                // multiple handler when the editing control is reused
                txt.KeyPress -= new KeyPressEventHandler(txt_KeyPress);
                // Add a handler for the TextBox's KeyPress event
                txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
            // Write your validation code here
            // ...
  2. Create a custom class that inherits from the standard DataGridView control and override its ProcessDialogKey method. This method is designed to process each key event, even those
    that occur on the editing control. You can either handle the key presses inside of that overridden method, or raise an event of your own to which you can attach a separate handler method.

share|improve this answer
but i want rowindex & column index too. then how can i get the rowindex & column index in above code? –  priyanka Feb 26 '11 at 7:59
@priyanka: The simplest way is using the DataGridView.CurrentCell property. It will return the cell that is currently selected, which in turn exposes ColumnIndex and RowIndex properties. –  Cody Gray Feb 26 '11 at 8:04
how to set the e.keychar value to the datagridview current cell? because when i'm using the above events in my application then that time it shows the current cell as null value & because of which i'm not able to perform the my other operations. –  priyanka Feb 26 '11 at 9:46
please can any body help me? –  priyanka Feb 26 '11 at 10:53
@priyanka: when you get that cell. You just need to set e.Handled=false in Custom KeyPress Event. –  Itz.Irshad Sep 20 '12 at 12:45

Your Answer


