ChrisJJ
ChrisJJ

Reputation: 2302

How may I test the type of a DataGridViewCell?

I tried to test whether 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?

Upvotes: 0

Views: 535

Answers (2)

Igby Largeman
Igby Largeman

Reputation: 16747

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).

Upvotes: 3

SLaks
SLaks

Reputation: 887887

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.

Upvotes: 2

Related Questions