簡體   English   中英

檢查DataGridView單元格值(字母或數字)

[英]Check DataGridView Cell Value (Alphabetic or Numeric)

如何檢查數據網格視圖的所選單元格中的值是字母還是數字?

您可以使用正則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");
  }

   ..... 

您可以像下面的代碼一樣使用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 ");
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM