-3

Whilst running the below code, I get this exception:

Cannot implicitly convert System.Eventhandler to System.Window.Form.KeyPressEventHandler

    private void grdPOItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        int colIndex = grdPOItems.CurrentCell.ColumnIndex;
        string colName = grdPOItems.Columns[colIndex].Name;
        if(colName == "Qty" || colName == "Rate")
        {
            e.Control.KeyPress += new EventHandler(CheckKey);
        }
    }

    private void CheckKey(object sender, EventArgs e)
    {
        if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar!='.'))
        {
            e.Handled = true;
        }  
    }
Draken
  • 3,134
  • 13
  • 34
  • 54
  • What are you trying to achieve? – cverb Jul 05 '16 at 09:01
  • Possible duplicate of [Cannot implicitly convert type 'System.EventHandler' to 'System.EventHandler' for storyboard complete](https://stackoverflow.com/questions/16636830/cannot-implicitly-convert-type-system-eventhandler-to-system-eventhandlerobj) –  May 30 '17 at 17:16

1 Answers1

1

According to the MSDN, your handler has the wrong signature. Use this instead.

 e.Control.KeyPress += CheckKey;

private void CheckKey(object sender, KeyPressEventArgs  e)
{
    if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar!='.'))
    {
        e.Handled = true;
    }  
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35