aspnet-devexpress-dot-web-dot-data.md
Provides data for the ASPxGridView.RowValidating event.
Namespace : DevExpress.Web.Data
Assembly : DevExpress.Web.v25.2.dll
NuGet Package : DevExpress.Web
public class ASPxDataValidationEventArgs :
ASPxGridDataValidationEventArgs
Public Class ASPxDataValidationEventArgs
Inherits ASPxGridDataValidationEventArgs
ASPxDataValidationEventArgs is the data class for the following events:
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:
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();
}
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
Object EventArgs ASPxGridDataValidationEventArgs ASPxDataValidationEventArgs
See Also