DataGridView控制单元格修改的输入规则

        // 修改值
        private void dgvOrderIn_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            DataGridViewTextBoxEditingControl CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
            CellEdit.SelectAll();
            CellEdit.KeyPress -= new KeyPressEventHandler(Cells_Number_KeyPress);
            CellEdit.KeyPress -= new KeyPressEventHandler(Cells_Price_KeyPress);
            switch (this.dgvOrderIn.CurrentCellAddress.X)
            {
                case 4: //数量
                    CellEdit.KeyPress += new KeyPressEventHandler(Cells_Number_KeyPress);
                    break;
                case 3: //单价
                case 5: //折扣率
                    CellEdit.KeyPress += new KeyPressEventHandler(Cells_Price_KeyPress);
                    break;
                default:
                    break;
            }
        }

        // 键盘输入数量控制
        private void Cells_Number_KeyPress(object sender, KeyPressEventArgs e)
        {
            int kc = Convert.ToInt32(e.KeyChar);
            // 8    backspace退格
            // 13   回车
            // 46   .
            // 48   0
            // 57   9 
            if ((kc < 48 || kc > 57) && kc != 8 && kc != 13)
            {
                e.Handled = true;
            }
            TextBox tb = (TextBox)sender;
            string strMathchValue = tb.Text;
            //判断是否为退格键
            if (kc == 8)
            {
                ///判断输入控件选择文本的长度 
                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(((TextBox)sender).SelectionStart, e.KeyChar.ToString());
            }

            Regex rx = new Regex(@"^\+?[1-9][0-9]*$", RegexOptions.IgnoreCase);
            if (rx.IsMatch(strMathchValue) == false)
            {
                e.Handled = true;
            }
        }

        // 键盘输入价格控制
        private void Cells_Price_KeyPress(object sender, KeyPressEventArgs e)
        {
            int kc = Convert.ToInt32(e.KeyChar);
            if ((kc < 48 || kc > 57) && kc != 8 && kc != 13 && kc != 46)
            {
                e.Handled = true;
            }
            TextBox tb = (TextBox)sender;
            string strMathchValue = tb.Text;
            if (kc == 8)
            {
                if (tb.SelectionLength == 0)
                {
                    if (tb.SelectionStart > 0)
                    {
                        strMathchValue.Remove(tb.SelectionStart - 1, 1);
                    }
                }
                else
                {
                    strMathchValue.Remove(tb.SelectionStart, tb.SelectionLength);
                }
            }
            else
            {
                strMathchValue = strMathchValue.Insert(((TextBox)sender).SelectionStart, e.KeyChar.ToString());
            }
            Regex rx = new Regex(@"^[0-9]+\.{0,1}[0-9]{0,2}$", RegexOptions.IgnoreCase);
            if (rx.IsMatch(strMathchValue) == false)
            {
                e.Handled = true;
            }
        }
原文地址:https://www.cnblogs.com/linji/p/2738249.html