locked
keypress event in Datagridview RRS feed

  • Question

  •  

    I am a student in Visual c# ( express edition2005 ) using windows xp + SP2

     

    I have a DataGridView with 9 rows and 9 columns

    KeyPress event handler is there for the DataGridView  but it is never raised! That is when I press any key the eventhandler for KeyPress is not activated( checked with breakpoint and debug);

    Need this for input in a DataGridviewCell.

     

    Please what have I missed?

     

      

    Sunday, March 2, 2008 10:31 PM

Answers

  • The DataGridView control does not pass you the KeyPress events while you are editing a cell. This is due to the fact that events are generated by the control that is providing the actual cell editing and not the DataGridView itself.

     

    You have essentially two ways to overcome the problem:

    1) Subclass the DataGridView control and override the ProcessDialogKey method. Please notice that Express Editions don't provide a template for Custom Controls, but they work perfectly.

     

    2) make your KeyPress method intercept also keys generated by the editing control. Looks like an hack, but works:

     

    you should already have the following method attached to the DataGridView:

    private void dataGridView1_KeyPress (object sender, KeyPressEventArgs e) { ... }

     

    attach the event EditingControlShowing with the following code:

    private void dataGridView1_EditingControlShowing (object sender, DataGridViewEditingControlShowingEventArgs e) {

      e.Control.KeyPress += new KeyPressEventHandler (dataGridView1_KeyPress);

    }

     

    You can obviously also have two separate methods.

     

    HTH

    --mc

     

    Monday, March 3, 2008 12:35 AM

All replies

  • The DataGridView control does not pass you the KeyPress events while you are editing a cell. This is due to the fact that events are generated by the control that is providing the actual cell editing and not the DataGridView itself.

     

    You have essentially two ways to overcome the problem:

    1) Subclass the DataGridView control and override the ProcessDialogKey method. Please notice that Express Editions don't provide a template for Custom Controls, but they work perfectly.

     

    2) make your KeyPress method intercept also keys generated by the editing control. Looks like an hack, but works:

     

    you should already have the following method attached to the DataGridView:

    private void dataGridView1_KeyPress (object sender, KeyPressEventArgs e) { ... }

     

    attach the event EditingControlShowing with the following code:

    private void dataGridView1_EditingControlShowing (object sender, DataGridViewEditingControlShowingEventArgs e) {

      e.Control.KeyPress += new KeyPressEventHandler (dataGridView1_KeyPress);

    }

     

    You can obviously also have two separate methods.

     

    HTH

    --mc

     

    Monday, March 3, 2008 12:35 AM
  • Thursday, March 6, 2008 6:48 PM