码迷,mamicode.com
首页 > Web开发 > 详细

16.AspxGridView之数据校验

时间:2015-06-19 16:43:49      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:

行数据校验(编辑状态)
调用DoRowValidation()可以进行行数据校验。它实际上是触发RowValidating事件编写代码实现数据校验的。DoRowValidation方法对浏览状态的行无效,可以通过获取AspxGridView的IsEditing属性判断是否进入编辑状态。

RowValidating事件原型:
void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)

DevExpress.Web.Data.ASPxDataValidationEventArgs构造函数:
public ASPxDataValidationEventArgs(bool isNew);

DevExpress.Web.Data.ASPxDataValidationEventArgs属性:
Errors: Dictionary<GridViewColumn, string >,行错误集合,只读。
HasErrors:bool,待处理的行是否有错。只读。
IsNewRow:bool,待处理的行是否新增行。只读。
Keys:OrderedDictionary,要删除行的主键栏位值。只读。
NewValues: OrderedDictionary,要删除行的非主键(实际上是所有栏位)栏位新值。只读。
OldValues: OrderedDictionary,要删除行的非主键(实际上是所有栏位)栏位原值。只读。
RowError:string,行错误信息。可读写。

通常在RowValidating事件中检查栏位值是否有效,如果无效,应抛出异常。
例:

//行数据检验事件
protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
{    
    //是否新增行
    if (e.IsNewRow)
    {
        if (e.NewValues["Event"] == null)
        {                    
            e.Errors.Add(this.ASPxGridView1.Columns["Event"], "事件名称不可为空");
            e.RowError = "事件名称不可为空";
            throw new Exception("事件名称不可为空");

        }
    }
}


通常在HtmlRowPrepared事件中检查行是否有效并以不同的颜色来显示未通过校验的行。
通常在StartRowEditing事件中调用DoRowValidation方法触发数据校验。

以下是官方的示例代码:

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

 

来源:.net学习网
说明:所有来源为 .net学习网 的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!

16.AspxGridView之数据校验

标签:

原文地址:http://www.cnblogs.com/xumingyang/p/4589087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!