Back to Devexpress

ASPxGridView.DoRowValidation() Method

aspnet-devexpress-dot-web-dot-aspxgridview-6ce25ec1.md

latest6.7 KB
Original Source

ASPxGridView.DoRowValidation() Method

Validates the row currently being edited.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public void DoRowValidation()
vb
Public Sub DoRowValidation

Remarks

The DoRowValidation method raises the ASPxGridView.RowValidating event so you can manually specify whether row values are valid.

Calling the DoRowValidation method has no effect if the ASPxGridView is in browse mode. To identify whether the grid is in edit mode, use the ASPxGridView.IsEditing property.

Example

This example demonstrates how to check the validity of data entered by end-users into a row. Validation is implemented within the ASPxGridView.RowValidating event handler. In this sample, validation fails in the cases listed below:

  • field value(s) is empty;
  • Contact Name and/or Company Name fields are set to a single character.

The ASPxGridView.HtmlRowPrepared event is handled to paint the row’s contents red if the row is invalid. This event is raised for each data row when the corresponding row within the table has been created. This indicates rows within invalid data.

The ASPxGridView.StartRowEditing event is handled to display errors (if any) within the edited row when an end-user switches to an edit mode.

csharp
using DevExpress.Web.ASPxGridView;
using System.Collections.Generic;

protected void grid_RowValidating(object sender, 
DevExpress.Web.Data.ASPxDataValidationEventArgs e) {
    // Checks for null values.
    foreach (GridViewColumn column in grid.Columns) {
        GridViewDataColumn dataColumn = column as GridViewDataColumn;
        if (dataColumn == null) continue;
        if (e.NewValues[dataColumn.FieldName] == null)
            e.Errors[dataColumn] = "Value cannot be null.";
    }
    // Displays the error row if there is at least one error.
    if (e.Errors.Count > 0) e.RowError = "Please, fill all fields.";

    if (e.NewValues["ContactName"] != null && 
        e.NewValues["ContactName"].ToString().Length < 2) {
        AddError(e.Errors, grid.Columns["ContactName"], 
        "Contact Name must be at least two characters long.");
    }
    if (e.NewValues["CompanyName"] != null && 
    e.NewValues["CompanyName"].ToString().Length < 2) {
        AddError(e.Errors, grid.Columns["CompanyName"], 
        "Company Name must be at least two characters long.");
    }
    if (string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0) 
    e.RowError = "Please, correct all errors.";
}

void AddError(Dictionary<GridViewColumn, string> errors, 
GridViewColumn column, string errorText) {
     if(errors.ContainsKey(column)) return;
     errors[column] = errorText;
 }

protected void grid_HtmlRowPrepared(object sender, 
ASPxGridViewTableRowEventArgs e) {
    // Checks whether the generated row has the errors.
    bool hasError = e.GetValue("ContactName").ToString().Length <= 1;
    hasError = hasError || e.GetValue("CompanyName").ToString().Length <= 1;
    hasError = hasError || e.GetValue("Country") == null;
    // If the row has the error(s), its text color is set to red.
    if (hasError)
        e.Row.ForeColor = System.Drawing.Color.Red;
}

protected void grid_StartRowEditing(object sender, 
DevExpress.Web.Data.ASPxStartRowEditingEventArgs e) {
    // Validates the edited row if it isn't a new row,.
    if (!grid.IsNewRowEditing)
        grid.DoRowValidation();
}
vb
Imports DevExpress.Web.ASPxGridView
Imports System.Collections.Generic

Protected Sub grid_RowValidating(ByVal sender As Object, ByVal e As 
DevExpress.Web.Data.ASPxDataValidationEventArgs)
    ' Checks for null values.
    For Each column As GridViewColumn In grid.Columns
        Dim dataColumn As GridViewDataColumn = TryCast(column, GridViewDataColumn)
        If Not dataColumn Is Nothing AndAlso_ 
        (e.NewValues(dataColumn.FieldName) Is Nothing) Then
            e.Errors(dataColumn) = "Value cannot be null."
        End If
    Next column
    ' Displays the error row if there is at least one error.
    If e.Errors.Count > 0 Then
        e.RowError = "Please, fill all fields."
    End If

    If Not e.NewValues("ContactName") Is Nothing AndAlso_
    e.NewValues("ContactName").ToString().Length < 2 Then
        AddError(e.Errors, grid.Columns("ContactName"),_
        "Contact Name must be at least two characters long.")
    End If
    If Not e.NewValues("CompanyName") Is Nothing AndAlso_
    e.NewValues("CompanyName").ToString().Length < 2 Then
        AddError(e.Errors, grid.Columns("CompanyName"),_
       "Company Name must be at least two characters long.")
    End If
    If String.IsNullOrEmpty(e.RowError) AndAlso e.Errors.Count > 0 Then
        e.RowError = "Please, correct all errors."
    End If
End Sub

Private Sub AddError(ByVal errors As_
    Dictionary(Of GridViewColumn, String), ByVal column As GridViewColumn,_
    ByVal errorText As String)
     If Not errors.ContainsKey(column) Then
     errors(column) = errorText
     End If
End Sub

Protected Sub grid_HtmlRowPrepared(ByVal sender As Object,_
ByVal e As ASPxGridViewTableRowEventArgs)
    ' Checks whether the generated row has the errors.
    Dim hasError As Boolean = e.GetValue("ContactName").ToString().Length <= 1
    hasError = hasError OrElse e.GetValue("CompanyName").ToString().Length <= 1
    hasError = hasError OrElse e.GetValue("Country") Is Nothing
    ' If the row has the error(s), its text color is set to red.
    If hasError Then
        e.Row.ForeColor = System.Drawing.Color.Red
    End If
End Sub

Protected Sub grid_StartRowEditing(ByVal sender As Object,_
    ByVal e As DevExpress.Web.Data.ASPxStartRowEditingEventArgs)
    ' Validates the edited row if it isn't a new row,.
    If (Not grid.IsNewRowEditing) Then
        grid.DoRowValidation()
    End If
End Sub

See Also

IsEditing

RowValidating

Grid View

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace