You could try the following: 1 - Handle the DataGridView.EditingControlShowing event
private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
// Make sure we're getting the key press event for a text type editor...
if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
// Handle the editing control's keypress event...
(e.Control).KeyPress += DataGridViewTextBoxEditingControlTextBoxControl_KeyPress;
}
2 - Inside the keypress event, check for the keys you need and process them
private void DataGridViewTextBoxEditingControlTextBoxControl_KeyPress(object sender, KeyPressEventArgs e)
{
// Standard editing keys can be processed...
if (e.KeyChar == (char)Keys.Back)
{
e.Handled = false;
// No need for further validation...
return;
}
// Just evaluate the contents of the cell if the value type is int
// NOTE: _dataGridView here is a reference to the grid in context...
if (_dataGridView.CurrentCell.ValueType != typeof (int))
return;
// Validate the key char...
// NOTE: By specifying e.Handled = true, we are indicating that
// we absorbed the character and this key is not sent to the cell.
e.Handled = char.IsNumber(e.KeyChar);
}
Hope this helps...
/F - .NET Developer
modified on Wednesday, July 30, 2008 11:55 AM