欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » AspxGridView使用教程 » 正文

AspxGridView之数据校验

创建时间:2011年12月11日 22:35  阅读次数:(8012)
分享到:

行数据校验(编辑状态)
调用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学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf】

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 20+78=? 的结果(结果是:98)
结  果: