zwphot 2016-06-08 04:09 采纳率: 0%
浏览 2411

C# 自定义DataGridViewTextBoxCell只能输入数字

public class myDataGridViewTextBoxColumn : DataGridViewTextBoxColumn
{
public myDataGridViewTextBoxColumn()
{
myDataGridViewTextBoxCell obj = new myDataGridViewTextBoxCell();
this.CellTemplate = obj;
}

}

public class myDataGridViewTextBoxCell : DataGridViewTextBoxCell
{
    protected override void OnKeyPress(KeyPressEventArgs e, int rowIndex)
    {            
        //只能输入数字
        if (!(char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
        {
            e.Handled = true; //输入是数字
        }
        else
            return;

        base.OnKeyPress(e, rowIndex);
    }     

}

    测试无效,甚至根本不执行OnKeyPress。不知道哪里有问题?
  • 写回答

1条回答 默认 最新

  • 普通网友 2016-10-05 16:11
    关注

    定义DataGridViewTextBoxCell只能输入数字
    public class myDataGridViewTextBoxColumn : DataGridViewTextBoxColumn
    {
    public myDataGridViewTextBoxColumn()
    {
    myDataGridViewTextBoxCell obj = new myDataGridViewTextBoxCell();
    this.CellTemplate = obj;
    }

    }
    public class myDataGridViewTextBoxCell : DataGridViewTextBoxCell
    {
    protected override void OnKeyPress(KeyPressEventArgs e, int rowIndex)
    {

    //只能输入数字
    if (!(char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
    {
    e.Handled = true; //输入是数字
    }
    else
    return;

        base.OnKeyPress(e, rowIndex);
    }     
    

    }

    测试无效,甚至根本不执行OnKeyPress。不知道哪里有问题?
    
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作