チェックボックス型セルの値を int 型で処理する(MultiRowSheet)

文書番号 : 22207     文書種別 : 使用方法     最終更新日 : 2006/07/08
文書を印刷する
対象製品
El Tabelle for .NET 3.0J
詳細
C# でチェックボックス型セルを使用するとき、値を直接 int 型に変換すると例外が発生します。

[C#]
GrapeCity.Win.ElTabelle.Editors.CheckBoxEditor objCheckBoxEditor =
  new GrapeCity.Win.ElTabelle.Editors.CheckBoxEditor();
multiRowSheet1[0, 0, 0].Editor = objCheckBoxEditor;
int value = (int)multiRowSheet1[0, 0, 0].Value; // <-例外が発生する

これは、チェックボックス型セルは null を許容するため、値型の int 型に変換できないことが原因です。
値を int 型に変換するには、次のように Object 型と string 型を経由します。

[C#]
GrapeCity.Win.ElTabelle.Editors.CheckBoxEditor objCheckBoxEditor =
  new GrapeCity.Win.ElTabelle.Editors.CheckBoxEditor();
multiRowSheet1[0, 0, 0].Editor = objCheckBoxEditor;

int value = 0;
if (multiRowSheet1[0, 0, 0].Value != null)
{
  value = int.Parse(multiRowSheet1[0, 0, 0].Value.ToString());
}
Console.WriteLine(value);


なお、Visual Basic では、チェックボックス型セルの値を直接 int 型に変換して使用できます。

[Visual Basic]
Dim objCheckBoxEditor As GrapeCity.Win.ElTabelle.Editors.CheckBoxEditor = _
  new GrapeCity.Win.ElTabelle.Editors.CheckBoxEditor()
MultiRowSheet1(0, 0, 0).Editor = objCheckBoxEditor

Dim value As Integer = CType(MultiRowSheet1(0, 0, 0).Value, Integer)
Console.WriteLine(value)
関連情報
キーワード
HowTo

この文書は、以前は次のFAQ IDで公開されていました : 9443