MultiRow Windows Forms > Developer's Guide > Using MultiRow > User Input Validation > User-Defined Cell Validator |
You can create a user-defined cell validator by inheriting the CellValidator class.
The following code implements a cell validator to validate the maximum length of a cell's value.
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; } } |
The following code shows an example for a user-defined 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; |
You can use the designer's CellValidator collection editor to add a user-defined cell validator.
Use the following steps to add the MaxLengthValidator.