2011-09-19 94 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,因此编译器会给您一个警告。