private DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit
private void dgvConsume_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
CellEdit.SelectAll();
CellEdit.KeyPress += Cells_KeyPress;
}
private void Cells_KeyPress(object sender, KeyPressEventArgs e)
{
if (dataGridView1.Columns[dgvConsume.CurrentCell.ColumnIndex].Name.Equals("kjmoney"))
{
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;
}
}
}
}
转载于:https://www.cnblogs.com/xalyf/p/3333530.html