2011-09-19 18 views
0

私はmyDataGridViewCellがDataGridViewCheckBoxCellDataGridViewCellのタイプをテストするにはどうすればよいですか?

if(myDataGridViewCell.ValueType is DataGridViewCheckBoxCell) ... 

であるかどうかをテストしようとしたが、これは警告与える:

与えられた式は決して提供「System.Windows.Forms.DataGridViewCheckBoxCell」のではありません)

を入力します

どのようにDataGridViewCellのタイプをテストできますか?

答えて

2

ValueTypeは、セルが保持するデータ値のタイプです。それはあなたが望むものではありません。

ただやる、細胞の種類itslelfをテストするには:

if (myDataGridViewCell is DataGridViewCheckBoxCell) 
... 

DataGridViewCheckBoxCellとすべてのサブタイプのためにtrueになります)

または

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

(のために真となりますDataGridViewCheckBoxCellのみ)。

+0

チャールズとスラックス - ありがとう。 – ChrisJJ

2
if (myDataGridViewCell is DataGridViewCheckBoxCell) 

あなたのコードはValueType propertyの値がDataGridViewCheckBoxCellに変換可能であるかどうかをチェックしています。
ValueTypeは常にSystem.Typeインスタンスを保持しているため、決してDataGridViewCheckBoxCellではないため、コンパイラは警告を表示します。

関連する問題