This enable you to specify whether row data is valid and whether the row can be updated.
The RowValidating event is automatically raised when a row is about to be updated, and allows you to specify whether its data is valid. To manually validate the edited row, call the DoRowValidation method.
To obtain the edited row's values, use the event parameter's ASPxDataValidationEventArgs.NewValues
property. The old values are returned by the ASPxDataValidationEventArgs.OldValues property. If the
value is invalid, use the ASPxDataValidationEventArgs.Errors property to specify the error description
text. As a result, an error icon will be displayed next to the invalid value. Pointing to the icon shows the
hint with the error description.
using DevExpress.Web.ASPxGridView;
using System.Collections.Generic;
namespace Petshop.Core
{
class RowValidationDemo
{
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();
}
}
}
The HtmlRowPrepared event is handled to paint the row's contents red if the row is invalid. This event is raisedfor each data row when the corresponding row within the table has been created. This indicates rows within
invalid data.
The StartRowEditing event is handled to display erorrs (if any) within the edited row when an end-user
switches to edit mode.
Another Way is collect the exception at ASPxGridView CustomErrorText event and set the e.ErrorText property to show
the custom error text.
switches to edit mode.
Another Way is collect the exception at ASPxGridView CustomErrorText event and set the e.ErrorText property to show
the custom error text.
string customeErrorMessage = string.Empty;
if (e.Exception is ExceptionType)
{
customeErrorMessage = your message;
}
e.ErrorText = customeErrorMessage;
e.ErrorText = customeErrorMessage;
If you catch exceptions with a try ... catch the ASPxGridView see that all is well and not make the error message.
Thank you!
ReplyDelete