How may I test the type of a DataGridViewCell?
I tried to test wh开发者_高级运维ether myDataGridViewCell is a DataGridViewCheckBoxCell
if(myDataGridViewCell.ValueType is DataGridViewCheckBoxCell) ...
but this gives the warning:
The given expression is never of the provided 'System.Windows.Forms.DataGridViewCheckBoxCell') type
How can you test the type of a DataGridViewCell?
ValueType
is the type of the data values that the cell holds. That's not what you want.
To test the type of the cell itslelf, just do:
if (myDataGridViewCell is DataGridViewCheckBoxCell)
...
(will be true for DataGridViewCheckBoxCell
and all subtypes)
or
if (myDataGridViewCheckBoxCell != null &&
myDataGridViewCheckBoxCell.GetType() == typeof(DataGridViewCheckBoxCell))
...
(will be true for DataGridViewCheckBoxCell
only).
if (myDataGridViewCell is DataGridViewCheckBoxCell)
Your code is checking whether the value of the ValueType
property is convertible to DataGridViewCheckBoxCell
.
Since ValueType
always holds a System.Type
instance, it's never a DataGridViewCheckBoxCell
, so the compiler gives you a warning.
精彩评论