如何限制DataGridView中某列只能输入汉字?同时可以限定字数?

解决方案 »

  1.   

    正则表达式^[\u4e00-\u9fa5]{0,n}$, n为最大字数
      

  2.   

    使用js正则来匹配
    this.txtGrvText.onchange=function(){
       var regex=@"^[\u4e00-\u9fa5]{0,n}$";
       if(regex.text(this.value))
       {
           alert('请输入汉字');
           return;
       }
    };
      

  3.   

    我在CellValidating事件中如下处理,但没有效果,不知为何?      if (Regex.IsMatch(e.ToString(), @"^[\u4e00-\u9fa5]+$") == true)
          {
            e.Cancel = false;
          }
      

  4.   

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)  
      {  
      dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;  
      if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "")  
      {  
      if(Regex.IsMatch(e.FormattedValue.ToString(), @"^[\u4e00-\u9fa5]+$") )  
      {   
       e.Cancel = true;  
      }  
      }  
      }