dataGridview中单元格的值只允许输入数字并且随时捕获到用户输入的每一个数字??
怎么实现??

解决方案 »

  1.   

    cell_enter事件,或者cell_keypress事件,不记得那个了
    然后你在这个事件里面,判断列
    如果是要的这一列值,你就验证是否只是数字
    只是数字的话,可以通过,否则,,
    貌似有比这更好的效果
    以前做过,没备份,不记得了
    也好久没写过winform的事件了
      

  2.   

    但是我在keypress事件里面写了验证的,单元格编辑时没有触发这个事件。。
    ??有点奇怪
      

  3.   

    有个属性Mask 选择 00000
      

  4.   

    dataGridView1_KeyPress?
    dataGridView1_KeyDown?
    dataGridView1_KeyUp?
    这三个我都试过了,都不行。
    要写到哪个事件呢?
      

  5.   

    datagridview-->编辑列--》DefaultCellStyle-->Format-->Numeric试一下。
      

  6.   

      using System.Text.RegularExpressions;
      static   private   Regex   r   =   new   Regex("^[0-9]{1,}$");       //这个可以写成静态的,就不用老是构造      private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 3)//第四列的值为数字
                {
                  if(!r.IsMatch(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))   
                  {   
                      MessageBox.Show("请输入数字");
                      dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
                  }                   
                }
            }试试 呵呵
      

  7.   

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
            { 
                double d= 0.0; 
                dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty; 
                if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "") 
                { 
                    if(!double.TryParse(e.FormattedValue.ToString(),out d) ) 
                    { 
                        dataGridView1.Rows[e.RowIndex].ErrorText = "数量输入有误!"; 
                        e.Cancel = true; 
                    } 
                } 
            } 
    参考