datagridview 限制某列只能输入数字(方法一)

来源:互联网 发布:个人博客整站源码 编辑:程序博客网 时间:2024/05/22 03:31

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;    }}
0 0
原创粉丝点击