我试图测试myDataGridViewCell是否是DataGridViewCheckBoxCell我该如何测试DataGridViewCell的类型?
if(myDataGridViewCell.ValueType is DataGridViewCheckBoxCell) ...
但这给出警告:
给定表达式是从来没有提供的“System.Windows.Forms.DataGridViewCheckBoxCell”的)类型
如何测试DataGridViewCell的类型?
我试图测试myDataGridViewCell是否是DataGridViewCheckBoxCell我该如何测试DataGridViewCell的类型?
if(myDataGridViewCell.ValueType is DataGridViewCheckBoxCell) ...
但这给出警告:
给定表达式是从来没有提供的“System.Windows.Forms.DataGridViewCheckBoxCell”的)类型
如何测试DataGridViewCell的类型?
ValueType
是单元格保存的数据值的类型。这不是你想要的。
检测细胞itslelf的类型,只是做:
if (myDataGridViewCell is DataGridViewCheckBoxCell)
...
(会真为DataGridViewCheckBoxCell
和所有亚型)
或
if (myDataGridViewCheckBoxCell != null &&
myDataGridViewCheckBoxCell.GetType() == typeof(DataGridViewCheckBoxCell))
...
(将成为真正的DataGridViewCheckBoxCell
)。
if (myDataGridViewCell is DataGridViewCheckBoxCell)
您的代码检查ValueType
property的值是否可以转换为DataGridViewCheckBoxCell
。
由于ValueType
始终保存一个System.Type
实例,因此它从来不是DataGridViewCheckBoxCell
,因此编译器会给您一个警告。
Charles和SLacks - 谢谢。 – ChrisJJ