Discussion:
DataGridViewCell.OnKeyPress not called
(too old to reply)
Bob Cave
2009-10-08 20:28:01 UTC
Permalink
I would like to intercept keystrokes typed into the cells of a DataGridView
control to prevent non-numeric data from being entered in the cell. I have
derived a class from DataGridViewCell and implemented the OnKeyPress method,
but when I type into the cells, OnKeyPress is not being called. Here is the
code I am using:

// In the form constructor, I call the following code to set up a new
column in the grid.
DataGridViewNumericColumn columnX = new
DataGridViewNumericCellColumn("X");
m_dataGrid.Columns.Add(m_columnX);

public class DataGridViewNumericCell : DataGridViewTextBoxCell
{
protected override void OnKeyPress(KeyPressEventArgs e, int rowIndex)
{
// Logic to exclude non-numeric keystrokes goes here.
base.OnKeyPress(e, rowIndex);
}
}

public class DataGridViewNumericCellColumn : DataGridViewColumn
{
public DataGridViewNumericCellColumn(String colName)
{
this.CellTemplate = new DataGridViewNumericCell();
this.Name = colName;
this.Width = 125;
}

public DataGridViewNumericCellColumn()
{
this.CellTemplate = new DataGridViewNumericCell();
}
}

First, is this the best way to accomplish what I am trying to do? Second,
why doesn't the OnKeyPress() method get called?

Thanks,

Bob
John Bundy
2009-10-09 14:59:06 UTC
Permalink
Change your forms KeyPreview to true, then use a form level keypress event.
What the KeyPreview does is makes sure all keystrokes are registered with the
form. I don't really know why the KeyPress event is there for the datagrid as
i could not get it to raise intuitively. Here is a sample of my keypress
code, note it is in VB.

If EmployeesDataGridView.CurrentCell.IsInEditMode Then
MsgBox(e.KeyValue)
End If
--
-John http://www.jmbundy.blogspot.com/
Please rate when your question is answered to help us and others know what
is helpful.
Post by Bob Cave
I would like to intercept keystrokes typed into the cells of a DataGridView
control to prevent non-numeric data from being entered in the cell. I have
derived a class from DataGridViewCell and implemented the OnKeyPress method,
but when I type into the cells, OnKeyPress is not being called. Here is the
// In the form constructor, I call the following code to set up a new
column in the grid.
DataGridViewNumericColumn columnX = new
DataGridViewNumericCellColumn("X");
m_dataGrid.Columns.Add(m_columnX);
public class DataGridViewNumericCell : DataGridViewTextBoxCell
{
protected override void OnKeyPress(KeyPressEventArgs e, int rowIndex)
{
// Logic to exclude non-numeric keystrokes goes here.
base.OnKeyPress(e, rowIndex);
}
}
public class DataGridViewNumericCellColumn : DataGridViewColumn
{
public DataGridViewNumericCellColumn(String colName)
{
this.CellTemplate = new DataGridViewNumericCell();
this.Name = colName;
this.Width = 125;
}
public DataGridViewNumericCellColumn()
{
this.CellTemplate = new DataGridViewNumericCell();
}
}
First, is this the best way to accomplish what I am trying to do? Second,
why doesn't the OnKeyPress() method get called?
Thanks,
Bob
Loading...