GrapeCity MultiRow Windows Forms Documentation
User-Defined Cell Validator

You can create a user-defined cell validator by inheriting the CellValidator class.

Using Code

The following code implements a cell validator to validate the maximum length of a cell's value.

[VB]

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

[CS]

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.

[VB]

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

[CS]

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;

Designer and User-Defined Cell Validator

You can use the designer's CellValidator collection editor to add a user-defined cell validator.

Use the following steps to add the MaxLengthValidator.

  1. Create a user-defined cell validator MaxLengthValidator and add it to the project.
  2. Build the project.
  3. Select the cell for which you wish to validate the value (for example: textBoxCell1).
  4. Select the Validators property from the property window, and click the ellipses ... button.
  5. Select MaxLengthValidator from the combobox on the top-left of the displayed CellValidator collection editor, and click Add.
  6. Select the MaxLength property from the property grid on the right of the screen, and enter 5.
  7. Select the Actions property from the property grid on the right of the sceen, and click the ellipses ... button.
  8. Add LineNotify in the displayed CellValidateAction collection editor.
  9. Click the OK button and close the CellValidateAction collection editor.
  10. Click the OK button and close the CellValidator collection editor.
See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Support Options