做具体列只接受重点preSS事件datagridview的数值 [英] Make a specific column only accept numeric value in datagridview in Keypress event

查看:91
本文介绍了做具体列只接受重点preSS事件datagridview的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的DataGridView只只在关键preSS事件接受特定的列数值。有没有做到这一点的最好办法?

I need to make datagridview that only accept the numeric value for specific column only in keypress event. Is there any best way to do this?

推荐答案


  • 添加EditingControlShowing的事件

  • 在EditingControlShowing,检查,如果当前单元格位于所需列。

  • 在EditingControlShowing(如果以上条件为真)登记键preSS的一个新的事件。

  • 删除任意键preSS事件EditingControlShowing增加previously。

  • 在重点preSS事件,检查如果key不是数字则取消输入。

例如:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
    if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column
    {
        TextBox tb = e.Control as TextBox;
        if (tb != null)
        {
            tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);
        }
    }
}

private void Column1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}

这篇关于做具体列只接受重点preSS事件datagridview的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆