Kevin Cheng's Yard
电脑是我的老婆,编程是我的灵魂,代码是我的语言,按键是我在歌唱。
随笔- 54  文章- 0  评论- 249 
博客园  首页  新随笔  联系  管理  订阅 订阅

DevExpress ASPxGridView 使用文档五:事件

转载请注明出处:http://surfsky.cnblogs.com/

---------------------------------------------------------
-- ASPxGridView 事件
---------------------------------------------------------
ASPxGridView
    默认是以callback方式(ajax方式)传递数据给服务器来实现局部刷新功能的
    若要改为postback方式,可设置 EnableCallBacks = "false"


---------------------------------------------------------
展示视图
---------------------------------------------------------
RowCreated(创建行数据时触发,类似 GridView 的 DataItemCreate 事件)
    protected void grid_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
    {
        if(e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
        // 设置模板列lable控件值
        Label label = grid.FindRowCellTemplateControl(e.VisibleIndex, null, "changePercent") as Label;
        decimal change = (decimal)grid.GetRowValues(e.VisibleIndex, "Change");
        label.Text = string.Format("{0:p}", change);
        // 设置模板列image控件的图像
        System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)grid.FindRowCellTemplateControl(e.VisibleIndex, null, "changeImage");
        img.Visible = false;
        if(change != 0) {
            img.Visible = true;
            img.ImageUrl = change < 0 ? "~/Images/arRed.gif" : "~/Images/arGreen.gif";
            label.ForeColor = change < 0 ? Color.Red : Color.Green;
        }
    }

HtmlRowPrepared(行准备?可在此设置行的展示效果,如背景)
    protected void grid_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
    {
        bool hasError = e.GetValue("FirstName").ToString().Length <= 1;
        hasError = hasError || e.GetValue("LastName").ToString().Length <= 1;
        hasError = hasError || !e.GetValue("Email").ToString().Contains("@");
        hasError = hasError || (int)e.GetValue("Age") < 18;
        DateTime arrival = (DateTime)e.GetValue("ArrivalDate");
        hasError = hasError || DateTime.Today.Year != arrival.Year || DateTime.Today.Month != arrival.Month;
        if(hasError) {
            e.Row.ForeColor = System.Drawing.Color.Red;
        }
    }

UnboundColumnData (非绑定列数据填充)
    protected void grid_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
    {
        if(e.Column.FieldName == "Total")
        {
            decimal price = (decimal)e.GetListSourceFieldValue("UnitPrice");
            int quantity = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"));
            e.Value = price * quantity;
        }
    }

CustomColumnDisplayText(定制列文本展示)
     protected void grid_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e)
     {
         if(object.Equals(e.Column, grid.Columns["Size"]))
             e.DisplayText = GetSizeDisplayText(e.Value);
     }

SummaryDisplayText(合计行文本展示)
     protected void grid_SummaryDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewSummaryDisplayTextEventArgs e) {
         if(e.Item.FieldName == "Size") {
             e.Text = GetSizeDisplayText(e.Value);
         }
     }

HeaderFilterFillItems(自定义过滤器处理逻辑)
    protected void grid_HeaderFilterFillItems(object sender, ASPxGridViewHeaderFilterEventArgs e)
    {
        if(object.Equals(e.Column, grid.Columns["Total"])) {
            PrepareTotalFilterItems(e);
            return;
        }
        if(object.Equals(e.Column, grid.Columns["Quantity"])) {
            PrepareQuantityFilterItems(e);
            return;
        }
    }

---------------------------------------------------------
回调处理
---------------------------------------------------------
CustomCallback(Ajax 回调处理)
    <select id="selGridLayout" onchange="grid.PerformCallback(this.value);" >
        <option selected="selected" value="0">Country</option>
        <option value="1">Country, City</option>
        <option value="2">Company Name</option>
    </select>
    protected void grid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        int layoutIndex = -1;
        if(int.TryParse(e.Parameters, out layoutIndex))
            ApplyLayout(layoutIndex); // 更换布局
    }

CustomButtonCallback(定制按钮的ajax回调处理)
    protected void grid_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e)
    {
        if(e.ButtonID != "Copy") return;
        copiedValues = new Hashtable();
        foreach(string fieldName in copiedFields)
            copiedValues[fieldName] = grid.GetRowValues(e.VisibleIndex, fieldName);
       
        grid.AddNewRow();
    }

 

---------------------------------------------------------
编辑视图
---------------------------------------------------------
InitNewRow(新建行的数据初始化处理)
    protected void grid_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
    {
        if(copiedValues == null) return;
        foreach(string fieldName in copiedFields) {
            e.NewValues[fieldName] = copiedValues[fieldName];
        }
    }


CellEditorInitialize(编辑器初始化)
    protected void grid_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {
        if(grid.IsEditing && !grid.IsNewRowEditing && e.Column.FieldName == "City")
        {
            string country = (string)grid.GetRowValuesByKeyValue(e.KeyValue, "Country");
            ASPxComboBox combo = e.Editor as ASPxComboBox;
            FillCityCombo(combo, country);
            combo.Callback += new CallbackEventHandlerBase(cmbCity_OnCallback);
        }
    }

StartRowEditing(开始编辑)
    protected void grid_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
    {
        if(!grid.IsNewRowEditing) {
            grid.DoRowValidation();
        }
    }

RowValidating (行数据验证)
    protected void grid_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
    {
        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 can't be null.";
            }
        }
        if(e.Errors.Count > 0) e.RowError = "Please, fill all fields.";
        if(e.NewValues["FirstName"] != null && e.NewValues["FirstName"].ToString().Length < 2) {
            AddError(e.Errors, grid.Columns["FirstName"], "First Name must be at least two characters long.");
        }
        if(e.NewValues["LastName"] != null && e.NewValues["LastName"].ToString().Length < 2) {
            AddError(e.Errors, grid.Columns["LastName"], "Last Name must be at least two characters long.");
        }
        if(e.NewValues["Email"] != null && !e.NewValues["Email"].ToString().Contains("@")) {
            AddError(e.Errors, grid.Columns["Email"], "Invalid e-mail.");
        }

        int age = 0;
        int.TryParse(e.NewValues["Age"] == null ? string.Empty : e.NewValues["Age"].ToString(), out age);
        if(age < 18) {
            AddError(e.Errors, grid.Columns["Age"],  "Age must be greater than or equal 18.");
        }
        DateTime arrival = DateTime.MinValue;
        DateTime.TryParse(e.NewValues["ArrivalDate"] == null ? string.Empty : e.NewValues["ArrivalDate"].ToString(), out arrival);
        if(DateTime.Today.Year != arrival.Year || DateTime.Today.Month != arrival.Month) {
            AddError(e.Errors, grid.Columns["ArrivalDate"], "Arrival date is required and must belong to the current month.");
        }

        if(string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0) e.RowError = "Please, correct all errors.";
    }

 

 

转载请注明出处:http://surfsky.cnblogs.com 

绿色通道:好文要顶关注我收藏该文与我联系
posted @ 2010-08-13 13:09 Kevin Cheng 阅读(1374) 评论(3) 编辑 收藏
发表评论
1934626
 回复 引用 查看   
#1楼2010-08-26 16:03 | yiran87      
很好,正是我需要的资料


 回复 引用 查看   
#2楼[楼主]2010-10-11 09:37 | Kevin Cheng      
对大家有用我就开心 :)
 回复 引用 查看   
#3楼2010-10-14 15:02 | cooltan      
同1楼,楼主好人啊
注册用户登录后才能发表评论,请 登录 或 注册,返回博客园首页。
首页博问闪存新闻园子招聘知识库
最新IT新闻:
· 谷歌将出售Clearwire股份 不到收购价1/10
· 为什么Google比苹果更令微软恐惧?
· 思科拟2.71亿美元收购私有公司Lightwire
· 社交商务公司Bazaarvoice将于2月24日进行IPO
· 戴尔收购备份软件公司AppAssure
» 更多新闻...
最新知识库文章:
· 领域模型管理与AOP
· 编程的艺术:漂亮的代码和漂亮的软件
· GIT分支管理是一门艺术
· 编程:是一门艺术
· 编程是一门艺术吗?
» 更多知识库文章...

China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
Copyright ©2012 Kevin Cheng
精于斯,乐于斯。
昵称:Kevin Cheng
园龄:6年3个月
粉丝:20
关注:1
<2010年8月>
日一二三四五六
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

搜索

 
 

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论
  • 我的标签
  • 更多链接

我的标签

  • Rss RssItem RssChannel XmlSerializer(1)
  • MVC ASP.NET Razor(1)
  • MVC Razor ASP.NET(1)
  • MVC ASP.NET(1)
  • javascript oo 类 对象 命名空间(1)
  • ASP.NET MVC SKIN 换肤(1)
  • Silverlight WPF(1)

随笔分类

  • 日子(3)
  • .NET(14)
  • .NET组件控件(15)
  • IT新闻(1)
  • 报表开发(1)
  • 代码生成器(1)
  • 工作流引擎
  • 建模与快速开发 (5)
  • 数据库 (4)
  • 杂项(7)

随笔档案

  • 2010年12月 (1)
  • 2010年11月 (1)
  • 2010年10月 (6)
  • 2010年8月 (7)
  • 2010年6月 (3)
  • 2009年12月 (1)
  • 2009年11月 (2)
  • 2009年5月 (2)
  • 2008年12月 (1)
  • 2008年8月 (1)
  • 2008年7月 (1)
  • 2007年12月 (1)
  • 2007年6月 (1)
  • 2007年5月 (1)
  • 2007年3月 (2)
  • 2007年1月 (1)
  • 2006年12月 (1)
  • 2006年11月 (1)
  • 2006年10月 (1)
  • 2006年9月 (1)
  • 2006年8月 (3)
  • 2006年6月 (5)
  • 2006年3月 (2)
  • 2006年2月 (1)
  • 2005年12月 (7)

文章分类

  • .NET(1)

相册

  • 回忆

Blogs

  • DbToCode
  • RapidTier
  • SmartPersistenceLayer
  • 灵感之源

NET WebSite

  • ASP.NET
  • CodeProject
  • CSDN
  • GoDotNet
  • MSDN
  • SourceForge

Special

  • icsharpcode.com
  • Open License
  • Python

最新评论

阅读排行榜

评论排行榜

推荐排行榜