zoukankan      html  css  js  c++  java
  • DataGirdView 编辑项时的验证

    dgvConfig.DataSource = CreateTable();
                dgvConfig.Columns["编号"].ReadOnly = true; //只读
                dgvConfig.AllowUserToAddRows = false;  //不允许添加新行
                dgvConfig.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dgvConfig_EditingControlShowing);
    

      

     void dgvConfig_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                DataGridViewTextBoxEditingControl cotrol = (DataGridViewTextBoxEditingControl)e.Control;
                cotrol.KeyPress += new KeyPressEventHandler(Edit_Value);
                cotrol.MaxLength = 5;
            }
    

      

    //DataGridView 编辑项输入
            protected void Edit_Value(object sender, KeyPressEventArgs e)
            {
                DataGridViewTextBoxEditingControl control = (DataGridViewTextBoxEditingControl)sender;
    
                if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
                {
                    e.Handled = true;//消除不合适字符  
                }
                else if (Char.IsPunctuation(e.KeyChar))
                {
                    if (e.KeyChar != '.' || control.Text.Length == 0)//小数点  
                    {
                        e.Handled = true;
                    }
                    if (control.Text.LastIndexOf('.') != -1)
                    {
                        e.Handled = true;
                    }
                }
            }
    

      

    #region  输入
    
            //允许输入小数
            public static void TextBox_Double_KeyPress(object sender, KeyPressEventArgs e)
            {
                TextBox tb = (TextBox)sender;
                if (tb == null)
                {
                    e.Handled = true;//消除不合适字符
                    return;
                }
                if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
                {
                    e.Handled = true;//消除不合适字符  
                }
                else if (Char.IsPunctuation(e.KeyChar))
                {
                    if (e.KeyChar != '.' || tb.Text.Length == 0)//小数点  
                    {
                        e.Handled = true;
                    }
                    if (tb.Text.LastIndexOf('.') != -1)
                    {
                        e.Handled = true;
                    }
                }
            }
    
            //只能输入整数
            public static void Number_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar != '')//这是允许输入退格键
                {
                    if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
                    {
                        e.Handled = true;
                    }
                }
            }
    
            //DataGridView 编辑项输入
            public static void DataGridView_Edit_Value(object sender, KeyPressEventArgs e)
            {
                DataGridViewTextBoxEditingControl control = (DataGridViewTextBoxEditingControl)sender;
    
                if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
                {
                    e.Handled = true;//消除不合适字符  
                }
                else if (Char.IsPunctuation(e.KeyChar))
                {
                    if (e.KeyChar != '.' || control.Text.Length == 0)//小数点  
                    {
                        e.Handled = true;
                    }
                    if (control.Text.LastIndexOf('.') != -1)
                    {
                        e.Handled = true;
                    }
                }
            }
    
            #endregion
  • 相关阅读:
    解决在Pycharm中无法显示代码提示的问题
    解决在使用pip list时出现DEPRECATION
    Pycharm 有些库(函数)没有代码提示
    Oracle 11.2.0.4 For Windows 64bit+32bit 数据库
    Windows系统下oracle数据库每天定时备份
    PowerDesigner表创建脚本双引号问题
    Oracle11g 创建数据库中问题处理(必须运行Netca以配置监听程序)
    名人名言
    项目管理
    项目管理心得:一个项目经理的个人体会、经验总结(zz)
  • 原文地址:https://www.cnblogs.com/gxivwshjj/p/3351683.html
Copyright © 2011-2022 走看看