Skip to main content

ASPxDataValidationEventArgs Class

Provides data for the ASPxGridView.RowValidating event.

Namespace: DevExpress.Web.Data

Assembly: DevExpress.Web.v24.2.dll

Declaration

public class ASPxDataValidationEventArgs :
    ASPxGridDataValidationEventArgs

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.

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();
}

Inheritance

See Also