DGV 指定列 只允许输入“数字和小数点(金额)” 和“数字”

本文介绍如何使用C#编程限制DataGridView控件中特定列只能输入数字和小数点,并确保输入的有效性符合金额格式的要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#region DGV指定列只允许输入数字和小数点(一个小数点)  类似金额
        public DataGridViewTextBoxEditingControl CellEdit { get; set; }//声明一个CellEdit

        public void gridEnter_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            CellEdit = (DataGridViewTextBoxEditingControl)e.Control;//赋值
            CellEdit.SelectAll();
            CellEdit.KeyPress += Cells_KeyPress;
        }

        // 自定义事件
        public void Cells_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (_frm.grizdEnter.CurrentCellAddress.X == 2 || _frm.gridEnter.CurrentCellAddress.X == 4) // 判断当前列是不是要控制的列
            //{
            if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == 46 || e.KeyChar == (Char)Keys.Back))
            {
                e.Handled = true;
                return;
            }

            System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)sender;//System.Windows.Forms.TextBox
            string strMathchValue = tb.Text;
            Regex rx = new Regex(@"^[0-9]*(.)?[0-9]*$", RegexOptions.IgnoreCase);

            //判断是否为退格键
            if (e.KeyChar == (Char)Keys.Back)
            {
                ///判断输入控件选择文本的长度 
                if (tb.SelectionLength == 0)
                {//未选文本中时处理
                    if (tb.SelectionStart > 0)
                    {//选择的开始位置大于0时处理
                        //strMathchValue.Remove(tb.SelectionStart, 1);

                        strMathchValue.Remove(tb.SelectionStart-1, 1);

          
                    }
                }
                else
                {//有选中文本时处理
                    strMathchValue.Remove(tb.SelectionStart, tb.SelectionLength);
                }
            }
            else
            {
                //将当前输入的字符号插入到原来的字符串中
                strMathchValue = strMathchValue.Insert(((System.Windows.Forms.TextBox)sender).SelectionStart, e.KeyChar.ToString());
            }
            if (rx.IsMatch(strMathchValue) == false)
            {
                e.Handled = true;
            }
            //}
        }
        #endregion

#region DGV键盘屏蔽事件  只允许输入数字
        public DataGridViewTextBoxEditingControl CellEdit = null; // 声明一个CellEdit   

        public void xDataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewTextBoxEditingControl)
            {
                CellEdit = (DataGridViewTextBoxEditingControl)e.Control; //赋值
                CellEdit.SelectAll();
                CellEdit.KeyPress += Cells_KeyPress; //绑定到事件
            }
        }
        //自定义事件
        public void Cells_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (_frm.xDataGridView1.CurrentCellAddress.X == 3) // 判断当前列是不是要控制的列)
            //{
            if ((Convert.ToInt32(e.KeyChar) < 48 || Convert.ToInt32(e.KeyChar) > 57) && Convert.ToInt32(e.KeyChar) != 46 && Convert.ToInt32(e.KeyChar) != 8 && Convert.ToInt32(e.KeyChar) != 13)
            {
                e.Handled = true; // 输入非法就屏蔽
            }
            else
            {
                if (Convert.ToInt32(e.KeyChar) == 46)
                {
                    e.Handled = true;
                }
            }
            //}
        }

        #endregion


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值