2011-09-19 90 views
0

我試圖測試myDataGridViewCell是否是DataGridViewCheckBoxCell我該如何測試DataGridViewCell的類型?

if(myDataGridViewCell.ValueType is DataGridViewCheckBoxCell) ... 

但這給出警告:

給定表達式是從來沒有提供的「System.Windows.Forms.DataGridViewCheckBoxCell」的)類型

如何測試DataGridViewCell的類型?

回答

2

ValueType是單元格保存的數據值的類型。這不是你想要的。

檢測細胞itslelf的類型,只是做:

if (myDataGridViewCell is DataGridViewCheckBoxCell) 
... 

(會真爲DataGridViewCheckBoxCell和所有亞型)

if (myDataGridViewCheckBoxCell != null && 
    myDataGridViewCheckBoxCell.GetType() == typeof(DataGridViewCheckBoxCell)) 
    ... 

(將成爲真正的DataGridViewCheckBoxCell)。

+0

Charles和SLacks - 謝謝。 – ChrisJJ

2
if (myDataGridViewCell is DataGridViewCheckBoxCell) 

您的代碼檢查ValueType property的值是否可以轉換爲DataGridViewCheckBoxCell
由於ValueType始終保存一個System.Type實例,因此它從來不是DataGridViewCheckBoxCell,因此編譯器會給您一個警告。