Edit this page

Validation

The validation events are meant to support the data validation of the user input. They occur when the new content entered by the user is about to be committed.

Currently the validation occurs on row and cell level. To get notified, you can use the following events exposed by the RadGridView control:

Validation Events Lifecycle

It is important to know that each one of the validation events is fired only when the user is trying to commit new data in the RadGridView control. They do not occur when the RadGridView loads data.

The CellValidating event occurs always before the CellValidated event when the edited cell is about to lose its focus. If the focus is moved to a cell in the same row, then no other events occur. If the focus is moved to a cell in another row, then both RowValidating and RowValidated are fired, containing the whole row data, including the new and the old values of the edited cells.

Both CellValidating and RowValidating events allow you to stop the commit process by setting the boolean property IsValid to False.

CellValidating Event

The CellValidating event occurs when a cell is about to commit new content. It is always raised before the CellValidated event, which is described in the next section. CellValidating gives you the power to stop the commit process on a cell level obeying some internal rules of your application. For example, a cell might contain only values between 0 and 12, and all other values are considered invalid.

The parameters passed to the validating event handler are two:

  • The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.

  • The event arguments of type Telerik.Windows.Controls.GridViewCellValidatingEventArgs. Some of its important properties are:

  • Cell - the committed cell.

  • Row - the committed row to which the cell belongs.

  • IsValid - you can use this property to control the commit process. If you set it to False the process will stop, the data will not be committed and CellValidated will not be raised.

  • NewValue - the new value that is about to be committed.

  • OldValue - the old value that will be replaced.

  • ErrorMessage - custom text message used to hint the user about the type of validation error. For example 'The entered value must be between 0 and 12'.

You can subscribe to the CellValidating event declaratively or in code-behind like this:

<telerik:RadGridView CellValidating="radGridView_CellValidating"/>

this.radGridView.CellValidating += radGridView_CellValidating;
AddHandler Me.radGridView.CellValidating, AddressOf radGridView_CellValidating

To stop the commit process of a cell just set the IsValid property of the GridViewCellValidatingEventArgs to False.

The code snippet below checks whether the value entered in the "CountryId" column falls between 0 and 12. If the rule is not satisfied then the commit process is cancelled.

private void radGridView_CellValidating(object sender, GridViewCellValidatingEventArgs e)
{
    if (e.Cell.Column.UniqueName == "CountryId")
    {
        int newValue = Int32.Parse(e.NewValue.ToString());
        if (newValue < 0 || newValue > 12)
        {
            e.IsValid = false;
            e.ErrorMessage = "The entered value must be between 0 and 12";
        }
    }
}
Private Sub radGridView_CellValidating(ByVal sender As Object, ByVal e As GridViewCellValidatingEventArgs)
    If e.Cell.Column.UniqueName = "CountryId" Then
        Dim newValue As Integer = Int32.Parse(e.NewValue.ToString())
        If newValue < 0 OrElse newValue > 12 Then
            e.IsValid = False
            e.ErrorMessage = "The entered value must be between 0 and 12"
        End If
    End If
End Sub

And here is how the error message is displayed:

CellValidated Event

The CellValidated event occurs when the cell has validated the new content. It is raised after the CellValidating event (when the IsValid is not set to False), described in the previous section.

The parameters passed to the validated event handler are two:

  • The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.

  • The event arguments of type Telerik.Windows.Controls.GridViewCellValidatedEventArgs.

You can subscribe to the CellValidated event declaratively or in code-behind like this:

<telerik:RadGridView CellValidated="radGridView_CellValidated"/>

this.radGridView.CellValidated += radGridView_CellValidated;
AddHandler Me.radGridView.CellValidated, AddressOf radGridView_CellValidated

RowValidating Event

The RowValidating event occurs when a row is about to commit new content. It is always raised before the RowValidated event, which is described in the next section. RowValidating gives you the power to stop the commit process on a row level obeying some internal rules of your application. For example, it might not be allowed for a row to contain equal values in two specific columns as well as any other kind of relations between the values of a single row.

The parameters passed to the validating event handler are two:

  • The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.

  • The event arguments of type Telerik.Windows.Controls.GridViewRowValidatingEventArgs. Some of its important properties are:

  • Row - the committed row. You may use the Item property of the row to work with the new values.

  • IsValid - you can use this property to control the commit process. If you set it to False the process will stop, the data will not be committed and RowValidated will not be raised.

  • OldValues - the old values that will be replaced.

You can subscribe to the RowValidating event declaratively or in code-behind like this:

<telerik:RadGridView RowValidating="radGridView_RowValidating"/>

this.radGridView.RowValidating += radGridView_RowValidating;
AddHandler Me.radGridView.RowValidating, AddressOf radGridView_RowValidating

To stop the commit process just set the IsValid property of the GridViewRowValidatingEventArgs to False, like this:

private void radGridView_RowValidating(object sender, GridViewRowValidatingEventArgs e)
{
    e.IsValid = false;
}
Private Sub radGridView_RowValidating(ByVal sender As Object, ByVal e As GridViewRowValidatingEventArgs)
    e.IsValid = False
End Sub

RowValidated Event

The RowValidated event occurs when the row has validated the new content. It is raised after the RowValidating event (when the IsValid is not set to False), described in the previous section.

The parameters passed to the validated event handler are two:

  • The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.

  • The event arguments of type Telerik.Windows.Controls.GridViewRowValidatedEventArgs.

You can subscribe to the RowValidated event declaratively or in code-behind like this:

<telerik:RadGridView RowValidated="radGridView_RowValidated"/>

this.radGridView.RowValidated += radGridView_RowValidated;
AddHandler Me.radGridView.RowValidated, AddressOf radGridView_RowValidated

See Also