如何判定datagridview中用户输入的字符类型呢, 比如datagridview某一列,如何判断用户输入的是字符,还是数字呢

解决方案 »

  1.   

    private  void  dataGridView1_CellValidating(object  sender,  DataGridViewCellValidatingEventArgs  e)  
                      {  
                          int i=  0.0;  
                         dataGridView1.Rows[e.RowIndex].ErrorText  =  string.Empty;  
                          if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName  =="")  
                              {  
                                if(!int.TryParse(e.FormattedValue.ToString(),out  s)  ||s <0)  
                                      {  
                                     dataGridView1.Rows[e.RowIndex].ErrorText  =  "输入有误!";  
                                          e.Cancel  =  true;  
                                      }  
                              }  
                      }  
        
      private  void  dataGridView1_CellEndEdit(object  sender,  DataGridViewCellEventArgs  e)  
                      {  
                              dataGridView1.Rows[e.RowIndex].ErrorText  =  string.Empty;  
                      }
      

  2.   

     int i=  0.0;  
                        dataGridView1.Rows[e.RowIndex].ErrorText  =  string.Empty;  
                        if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName  =="")  
                              {  
                                if(!int.TryParse(e.FormattedValue.ToString(),out i)||i <0)  
                                   {  
                                    dataGridView1.Rows[e.RowIndex].ErrorText="输入有误!";  
                                     e.Cancel  =  true;  
                                      }  
                              }  
      

  3.   

    this.KeyPreview = true;private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (char.IsDigit(e.KeyChar))  MessageBox.Show("数字");
        else if (char.IsLetter(e.KeyChar)) MessageBox.Show("字母");
        else if (char.IsWhiteSpace(e.KeyChar)) MessageBox.Show("空");
        // ...
    }
      

  4.   

    int i=  0.0;  
    这样子一句肯定会报错的, 这种方式验证,对于小数部分好像会失效.
      

  5.   

    正则控件是什么英文啊,我找不到呀,另外 if (char.IsDigit(e.KeyChar))  MessageBox.Show("数字");  只能验证整数呀, 我的目的是可以验证小数与整数, 该如何改呢,
      

  6.   

     if (char.IsDigit(e.KeyChar))  MessageBox.Show("数字");  只能检查整数呀,我打一个小数点都不行。