zoukankan      html  css  js  c++  java
  • DataGridView 输入数据验证格式(实例)

    在DataGridView属性里面添加dgvOne_CellValidating事件,然后根据需要以后。

       private void dgvOne_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            //可编辑的列2、3、4、5、6(实际显示的列减1)列需要输入0~9的自然数.
            if ( e.ColumnIndex == 2 || e.ColumnIndex == 3 || e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 6)
            {
                int outDb = 0;
                if (int.TryParse(e.FormattedValue.ToString(), out outDb))
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;//数据格式不正确则还原
                    MessageBox.Show("请输入0~9的自然数!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dgvOne.CancelEdit();
                }
            }

          //第二列验证时间格式
            if (e.ColumnIndex == 1)
            {
                DateTime  outDb =DateTime.Now;
                if (DateTime.TryParse(e.FormattedValue.ToString(), out outDb))
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;//数据格式不正确则还原
                    MessageBox.Show("输入日期格式不正确,请重新输入!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dgvOne.CancelEdit();
                }
            }

                //第一列验证正整数

            if (e.ColumnIndex == 0)
            {
                Regex notWholePattern = new Regex(@"^[0-9]\d*$");
                if (notWholePattern.IsMatch(e.FormattedValue.ToString(), 0))
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;//数据格式不正确则还原
                    MessageBox.Show("输入序号的格式不正确,请重新输入正整数!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dgvTwo.CancelEdit();
                }
            }

           //长度验证

            if (e.ColumnIndex == 6 || e.ColumnIndex == 7)
            {
                string str = e.FormattedValue.ToString();
                int leng = str.Length;
                if (leng > 1)
                {
                    e.Cancel = true;//数据格式不正确则还原
                    MessageBox.Show("只能输入一位0~9的自然数,请重新输入!", "提交提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dgvTwo.CancelEdit();

                }
                else
                {
                    e.Cancel = false;
                }
            }

        }

  • 相关阅读:
    【Wyn Enterprise BI知识库】 什么是商业智能 ZT
    Wyn BI的机会在哪里:越靠近消费者的行业,比如零售、文娱和金融,信息化投入越大 ZT
    客户化软件时代的前夜 ZT
    在“非软件企业”开发软件的困局 ZT
    行业观察报告:从SAAS困局看行业趋势 ZT
    超级干货 :一文读懂数据可视化 ZT
    传统BI还是自助式BI---BI与数据分析 ZT
    【BI学习笔记】在Linux上安装Wyn Enterprise商业智能报表服务器
    MAMP 配置: Mac with OSX 10.8 + (Mac + Apache + MySQL + Php)
    Emule Xtreme Kid eD2K 设置
  • 原文地址:https://www.cnblogs.com/richzhang/p/3117898.html
Copyright © 2011-2022 走看看