c#,datagridview
添加EditingControlShowing事件
public DataGridViewTextBoxEditingControl CellEdit = null;
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.dataGridView1.CurrentCellAddress.X == 4)//获取当前处于活动状态的单元格索引
{
CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
CellEdit.SelectAll();
CellEdit.KeyPress += Cells_KeyPress; //绑定事件
}
}
private void Cells_KeyPress(object sender, KeyPressEventArgs e) //自定义事件
{
if ((this.dataGridView1.CurrentCellAddress.X == 4) || (this.dataGridView1.CurrentCellAddress.X == 5) || (this.dataGridView1.CurrentCellAddress.X == 6))//获取当前处于活动状态的单元格索引
{
if (!(e.KeyChar >= '0' && e.KeyChar <= '9')) e.Handled = true;
if (e.KeyChar == '\b') e.Handled = false;
}
}