RequiredTypeValidatorを使用すると、セルに入力された値が指定した型に適合するかどうかを検証できます。RequiredTypeValidatorは、DateTime、TimeSpanまたはDecimal型(数値型)のみをサポートしています。
デザイナで値の型の検証を設定するには、次のように操作します。この例では、文字列型セルに数値として変換できない文字列が入力されたときに検証エラーを表示します。
- 値を検証するセルを選択する。(例: textBoxCell1)
- プロパティウィンドウでValidatorsプロパティを選択し、[...]ボタンをクリックする。
- 表示されたCellValidatorコレクションエディタで左上のコンボボックスから「RequiredTypeValidator」を選択し、[追加]をクリックする。
- [メンバ]リストでRequiredTypeValidatorが選択されていることを確認する。
- 画面右のプロパティグリッドでRequiredTypeプロパティを選択し、数値を示すデータ型「Decimal」を選択する。
- 画面右のプロパティグリッドでActionsプロパティを選択し、[...]ボタンをクリックする。
- 表示されたCellValidateActionコレクションエディタでLineNotifyを追加する。
- [OK]ボタンをクリックしてCellValidateActionコレクションエディタを閉じる。
- [OK]ボタンをクリックしてCellValidatorコレクションエディタを閉じる。
- デザイナのドキュメントウィンドウのタブを「実行時」に切り替える。
- 非数値の文字列を入力してセルを移動したときに検証エラーになることを確認する。
次のコードは、Integer型として適合できない値が入力された場合に検証エラーを表示します。
Imports GrapeCity.Win.MultiRow
Dim TextBoxCell1 As New TextBoxCell()
TextBoxCell1.Value = "Hello"
Dim RequiredTypeValidator1 As New RequiredTypeValidator()
RequiredTypeValidator1.RequiredType = GetType(Decimal)
RequiredTypeValidator1.Actions.Add(New LineNotify())
TextBoxCell1.Validators.Add(RequiredTypeValidator1)
Dim cells As Cell() = {TextBoxCell1}
GcMultiRow1.Template = Template.CreateGridTemplate(cells)
GcMultiRow1.RowCount = 10
using GrapeCity.Win.MultiRow;
TextBoxCell textBoxCell1 = new TextBoxCell();
textBoxCell1.Value = "Hello";
RequiredTypeValidator requiredTypeValidator1 = new RequiredTypeValidator();
requiredTypeValidator1.RequiredType = typeof(decimal);
requiredTypeValidator1.Actions.Add(new LineNotify());
textBoxCell1.Validators.Add(requiredTypeValidator1);
Cell[] cells = { textBoxCell1 };
gcMultiRow1.Template = Template.CreateGridTemplate(cells);
gcMultiRow1.RowCount = 10;
RequiredTypeValidator.ParsingEnabledプロパティにTrueを設定すると、数値の文字列形式を等価な整数に変換して、変換した値に対して検証を行うことができます。また、RequiredTypeValidator.ParsingEnabledプロパティにTrueが設定されている場合、カルチャを指定するためのFormatProviderプロパティを使用できます。
次のコードは、セルに入力されたカンマ付きの数値を検証します。
Imports System.Globalization
Imports GrapeCity.Win.MultiRow
Dim textBoxCell1 As New TextBoxCell()
textBoxCell1.Style.Format = "#,#00"
textBoxCell1.ValueType = GetType(Int32)
Dim requiredTypeValidator1 As New RequiredTypeValidator()
requiredTypeValidator1.Actions.Add(New LineNotify())
requiredTypeValidator1.RequiredType = GetType(Decimal)
requiredTypeValidator1.ParsingEnabled = True
requiredTypeValidator1.FormatProvider = CultureInfo.CurrentCulture
textBoxCell1.Validators.Add(requiredTypeValidator1)
Dim cells As Cell() = {textBoxCell1}
GcMultiRow1.Template = Template.CreateGridTemplate(cells)
GcMultiRow1.RowCount = 10
using System.Globalization;
using GrapeCity.Win.MultiRow;
TextBoxCell textBoxCell1 = new TextBoxCell();
textBoxCell1.Style.Format = "#,#00";
textBoxCell1.ValueType = typeof(Int32);
RequiredTypeValidator requiredTypeValidator1 = new RequiredTypeValidator();
requiredTypeValidator1.Actions.Add(new LineNotify());
requiredTypeValidator1.NullIsValid = true;
requiredTypeValidator1.RequiredType = typeof(Decimal);
requiredTypeValidator1.ParsingEnabled = true;
requiredTypeValidator1.FormatProvider = CultureInfo.CurrentCulture;
textBoxCell1.Validators.Add(requiredTypeValidator1);
Cell[] cells = { textBoxCell1 };
gcMultiRow1.Template = Template.CreateGridTemplate(cells);
gcMultiRow1.RowCount = 10;