次のコードは、セルの値の最大文字数を検証するセルバリデータです。
Imports GrapeCity.Win.MultiRow
Public Class MaxLengthValidator
Inherits CellValidator
Protected Overrides Function Validate(ByVal context As GrapeCity.Win.MultiRow.ValidateContext) As Boolean
If context.EditedFormattedValue IsNot Nothing Then
If context.EditedFormattedValue.ToString().Length > Me.MaxLength Then
Return False
End If
End If
Return True
End Function
Public MaxLength As Integer
Public Overrides Function Clone() As GrapeCity.Win.MultiRow.CellValidator
Dim validator As MaxLengthValidator = TryCast(MyBase.Clone(), MaxLengthValidator)
validator.MaxLength = Me.MaxLength
Return validator
End Function
End Class
using GrapeCity.Win.MultiRow;
public class MaxLengthValidator : CellValidator
{
protected override bool Validate(ValidateContext context)
{
if (context.EditedFormattedValue != null)
{
if (context.EditedFormattedValue.ToString().Length > this.MaxLength)
return false;
}
return true;
}
public int MaxLength { get; set; }
public override CellValidator Clone()
{
MaxLengthValidator validator = base.Clone() as MaxLengthValidator;
validator.MaxLength = this.MaxLength;
return validator;
}
}
次のコードは、ユーザー定義のMaxLengthValidatorの使用例です。
Imports GrapeCity.Win.MultiRow
Dim TextBoxCell1 As New TextBoxCell
Dim MaxLengthValidator1 As New MaxLengthValidator()
MaxLengthValidator1.MaxLength = 2
MaxLengthValidator1.Actions.Add(New LineNotify())
TextBoxCell1.Validators.Add(MaxLengthValidator1)
Dim cells As Cell() = {TextBoxCell1}
GcMultiRow1.Template = Template.CreateGridTemplate(cells)
GcMultiRow1.RowCount = 10
TextBoxCell textBoxCell1 = new TextBoxCell();
MaxLengthValidator maxLengthValidator1 = new MaxLengthValidator();
maxLengthValidator1.MaxLength = 2;
maxLengthValidator1.Actions.Add(new LineNotify());
textBoxCell1.Validators.Add(maxLengthValidator1);
Cell[] cells = { textBoxCell1 };
gcMultiRow1.Template = Template.CreateGridTemplate(cells);
gcMultiRow1.RowCount = 10;
デザイナのCellValidatorコレクションエディタを使用してユーザー定義のセルバリデータを追加できます。

上記のMaxLengthValidatorを追加するには、次の手順を実行します。
- ユーザー定義のセルバリデータMaxLengthValidatorを作成し、プロジェクトに追加する。
- プロジェクトをビルドする。
- 値を検証するセルを選択する。(例: textBoxCell1)
- プロパティウィンドウでValidatorsプロパティを選択し、[...]ボタンをクリックする。
- 表示されたCellValidatorコレクションエディタで左上のコンボボックスからMaxLengthValidatorを選択し、追加をクリックする。
- 画面右のプロパティグリッドでMaxLengthプロパティを選択し、「5」を入力する。
- 画面右のプロパティグリッドでActionsプロパティを選択し、[...]ボタンをクリックする。
- 表示されたCellValidateActionコレクションエディタでLineNotifyを追加する。
- [OK]ボタンをクリックしてCellValidateActionコレクションエディタを閉じる。
- [OK]ボタンをクリックしてCellValidatorコレクションエディタを閉じる。