public DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit
private void dataGridView_Deal_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
CellEdit = (DataGridViewTextBoxEditingControl)e.Control; // 赋值 CellEdit.SelectAll();
CellEdit.KeyPress += Cells_KeyPress; // 绑定到事件
}
private void Cells_KeyPress(object sender, KeyPressEventArgs e)
{
if (dataGridView_Deal.CurrentCellAddress.X == 4 || dataGridView_Deal.CurrentCellAddress.X == 5) // 判断当前列是不是要控制的列 我是控制的索引值为2的 列(即第三列)
{
if ((Convert.ToInt32(e.KeyChar) < 48 || Convert.ToInt32(e.KeyChar) > 57) && Convert.ToInt32(e.KeyChar) != 46 && Convert.ToInt32(e.KeyChar) != 8 && Convert.ToInt32(e.KeyChar) != 13)
{
e.Handled = true; // 输入非法就屏蔽
}
else
{
//if ((Convert.ToInt32(e.KeyChar) == 46))
//{
// e.Handled = true;
//}
}
}
}