次のサンプルコードは、
CompareValueValidatorを使用して、元の値が10より大きいかどうかを検証します。入力モードに入って何らかの値を入力し、[Enter]キーを押して編集を終了します。その後で、[Esc]キーを押してCancelRowを発生させると、元の値が9であるため、検証が失敗してシステムサウンドが再生されます。このサンプルコードは、
CellValidatorに示されている詳細なコード例の一部を抜粋したものです。
void setCompareValueValidator_Click(object sender, EventArgs e)
{
Template template = Template.CreateGridTemplate(1);
CompareValueValidator compareValueValidator = new CompareValueValidator();
//You should set the RequiredType first, then set other Properties.
compareValueValidator.RequiredType = typeof(int);
compareValueValidator.ComparedValue = 10;
compareValueValidator.DifferenceValue = 0;
compareValueValidator.ComparedOperator = ValidateComparisonOperator.GreaterThan;
SoundNotify soundNotify = new SoundNotify();
soundNotify.SoundType = SystemSoundType.Asterisk;
//When cancel the whole Row editing, the old value will be restored.
soundNotify.DoActionReason = ValidateReasons.CancelRow;
compareValueValidator.Actions.Add(soundNotify);
//When do the SoundNotify action, this value 9 will be validated.
template.Row.Cells[0].Value = 9;
template.Row.Cells[0].Validators.Add(compareValueValidator);
this.gcMultiRow1.Template = template;
label.Text = "The CompareValueValidator.ComparedValue is 10, DifferenceValue is 0, ComparedOperator is GreaterThan, input some value to Cell, press ENTER to commit value(not validate), then press ESC to trigger validation(DoActionReason is CancelRow), the old value 9 is restored and validated, validation failed, one Sound will be played";
}
Private Sub setCompareValueValidator_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setCompareValueValidator.Click
Dim template As Template = template.CreateGridTemplate(1)
Dim compareValueValidator As New CompareValueValidator()
'You should set the RequiredType first, then set other Properties.
compareValueValidator.RequiredType = GetType(Integer)
compareValueValidator.ComparedValue = 10
compareValueValidator.DifferenceValue = 0
compareValueValidator.ComparedOperator = ValidateComparisonOperator.GreaterThan
Dim soundNotify As New SoundNotify()
soundNotify.SoundType = SystemSoundType.Asterisk
'When cancel the whole Row editing, the old value will be restored.
soundNotify.DoActionReason = ValidateReasons.CancelRow
compareValueValidator.Actions.Add(soundNotify)
'When do the SoundNotify action, this value 9 will be validated.
template.Row.Cells(0).Value = 9
template.Row.Cells(0).Validators.Add(compareValueValidator)
Me.gcMultiRow1.Template = template
label.Text = "The CompareValueValidator.ComparedValue is 10, DifferenceValue is 0, ComparedOperator is GreaterThan, input some value to Cell, press ENTER to commit value(not validate), then press ESC to trigger validation(DoActionReason is CancelRow), the old value 9 is restored and validated, validation failed, one Sound will be played"
End Sub