0

如何检查数据网格视图所选单元格中的值是字母还是数字?

4

2 回答 2

4

您可以使用Regex

  var input = ...;//Your cell content 
  var patternAlphabetic = @"([a-zA-Z])+";
  var patternNumeric = @"([0-9])+";

  var regex = new Regex(patternAlphabetic);
  var match = regex.Match(input);
  if (match.Success) 
  {
       System.Console.WriteLine("Alphabetic");
  }

   ..... 
于 2012-09-19T14:16:46.433 回答
2

您可以使用 typeof 运算符,如下代码:

        if ( typeof (Int32) == dataGridView1.SelectedCells[0].Value.GetType())
        {
            MessageBox.Show( "DataGridView Cell Value is Numeric" );
        }
        else if (typeof(string) == dataGridView1.SelectedCells[0].Value.GetType())
        {
            MessageBox.Show("DataGridView Cell Value is Alphabetic ");
        }
于 2012-09-19T14:35:32.690 回答