限制单元格输入的实现方法:
添加DataGridView的EditingControlShowing事件,代码如下:
private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(e.Control is DataGridViewTextBoxEditingControl tb)
{
DataGridView dgv = sender as DataGridView;
if (dgv.CurrentCell.ColumnIndex > 0)
{
tb.ShortcutsEnabled = false;
tb.ImeMode = ImeMode.Disable;
tb.SelectAll();
tb.KeyPress -= new KeyPressEventHandler(CellEdit_KeyPress);
tb.KeyPress += new KeyPressEventHandler(CellEdit_KeyPress);
}
}
}
软件需求,第一列输入不限制,输入汉字、英文、数字...都可以,第二列必须输入数字。
使用发现,只要激活第二列的单元格后,第一列就只能输入数字了。与实际需求相反
分析,整个DataGridView控件编辑单元格,只用一个DataGridViewTextBoxEditingControl 类型的控件实现输入。因此,激活第一列单元格后添加了限制。以后就被限制了。修改代码如下&#x