How to trap KeyPress events in a DataGridViewTextBox?

Giganews Newsgroups
Subject: How to trap KeyPress events in a DataGridViewTextBox?
Posted by:  Stewart Berman (saberman@nospam.nospam)
Date: Wed, 08 Apr 2009

I have a DataGridView control bound to a DataSet.  One of the columns is bound to a numeric field
(integer).  I want to trap the KeyPress event to prevent non numeric characters from being entered.

I have only been able to find the KeyPress event for the DataGridView control itself and it doesn't
seem to get control when a column field has the focus and a key is pressed and released.

I can trap the error in the CellValidating event but that does not stop the invalid character from
being entered -- it only stops the user from exiting the field after the invalid character is
already in the field and visible.

What am I missing?

Replies