New to Telerik UI for WinForms? Download free 30-day trial

Data Validation

RadVirtualGrid provides a convenient way to perform validation before data is committed to the associated data source. You can validate data by handling CellValidating event which is raised by RadVirtualGrid when the current cell changes or when the cell loses input focus (when pressing Enter key). Canceling this event prevents the user from exiting the cell until a valid editor value is entered or the edit process is canceled. The RowValidating event can be used the same way to prevent the user from exiting the current row.

Here is a list of all validation events:

  • CellValidating: Fires when a cell loses input focus, enabling content validation.

  • RowValidating: Fires when a row is validating.

  • RowValidated: Fires after a row has finished validating.

The VirtualGridViewInfo.SetRowErrorText method can be used to indicate validation errors. It will show an error indicator at the row header.

The code snippet below demonstrates simple data validation scenario. It is enabled in the third column to enter only non-empty strings. When the string is empty, the validation fails and the error indicator at the row header is shown:

Figure 1: Cell validation

WinForms RadVirtualGrid Cell validation


private void radVirtualGrid1_CellValidating(object sender, VirtualGridCellValidatingEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex == 2)
    {
        e.ViewInfo.SetRowErrorText(e.RowIndex, "Validation error!");
        if (string.IsNullOrEmpty((string)e.NewValue) || ((string)e.NewValue).Trim() == string.Empty)
        {
            e.Cancel = true;
            e.ViewInfo.SetRowErrorText(e.RowIndex, "Validation error!");
        }
        else
        {
            e.ViewInfo.ClearRowErrorText(e.RowIndex);
        }
    }
}

Private Sub radVirtualGrid1_CellValidating(sender As Object, e As VirtualGridCellValidatingEventArgs)
    If e.RowIndex >= 0 AndAlso e.ColumnIndex = 2 Then
        e.ViewInfo.SetRowErrorText(e.RowIndex, "Validation error!")
        If String.IsNullOrEmpty(DirectCast(e.NewValue, String)) OrElse DirectCast(e.NewValue, String).Trim() = String.Empty Then
            e.Cancel = True
            e.ViewInfo.SetRowErrorText(e.RowIndex, "Validation error!")
        Else
            e.ViewInfo.ClearRowErrorText(e.RowIndex)
        End If
    End If
End Sub

See Also

In this article