ASPxGridview自定义汇总(不同列的总数计算)

1、ASPxGridview设置GroupSummary(当然得把Footer这些打开)
2、开工率的SummaryType设置为Custom

                <GroupSummary>                    
                    <dx:ASPxSummaryItem DisplayFormat="{0}" FieldName="总合同数" ShowInGroupFooterColumn="总合同数" SummaryType="Sum" />
                    <dx:ASPxSummaryItem DisplayFormat="{0}" FieldName="已开工数" ShowInGroupFooterColumn="已开工" SummaryType="Sum" />
                    <dx:ASPxSummaryItem DisplayFormat="{0}" FieldName="开工率" ShowInGroupFooterColumn="开工率(%)" SummaryType="Custom" />
                </GroupSummary>

3、控件事件:OnCustomSummaryCalculate

    decimal _contract = 0;  //定义总合同数变量
    decimal _working = 0;   //定义开工数变量
    protected void ASPxGridView1_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e)
    {
        ASPxSummaryItem item = e.Item as ASPxSummaryItem;// 需要引用DevExpress.Web.ASPxGridView;
        if (item.FieldName == "开工率")//如果列为开工率的列
        {
            if (e.IsGroupSummary)//如果是分组
            {
                if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Start)//开始计算前
                {
                    _contract = 0;
                    _working = 0;
                    e.TotalValue = 0;
                }
                else if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Calculate)//计算
                {
                    _contract += Math.Round((decimal)e.GetValue("总合同数"), 2);//取总合同数进行累加赋值给公共变量
                    _working += Math.Round((decimal)e.GetValue("已开工数"), 2);//取已开工数进行累加赋值给公共变量
                }
                else if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Finalize)//计算完成
                {
                    e.TotalValue = Math.Round(_working / _contract * 100, 2);//开工/合同*100,取两位小数
                }
            }
        }
    } 

效果
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 ASP.NET 中,GridView 控件的可以在 aspx 页面中定义,也可以在 aspx.cs 文件中动态添加。如果您想在 aspx.cs 文件中添加 GridView 的,可以通过以下代码实现: ```csharp protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { // 创建 GridView 的模板 TemplateField tf = new TemplateField(); tf.HeaderText = "自定义"; tf.ItemTemplate = new GridViewTemplate(ListItemType.Item, "自定义名称"); // 将添加到 GridView 中 GridView1.Columns.Add(tf); } } // 自定义 GridView 模板的实现 public class GridViewTemplate : ITemplate { private ListItemType _templateType; private string _columnName; public GridViewTemplate(ListItemType type, string columnName) { _templateType = type; _columnName = columnName; } public void InstantiateIn(Control container) { switch (_templateType) { case ListItemType.Item: // 在 ItemTemplate 中添加控件 Label lbl = new Label(); lbl.ID = "lbl" + _columnName; container.Controls.Add(lbl); break; case ListItemType.EditItem: // 在 EditItemTemplate 中添加控件 TextBox txt = new TextBox(); txt.ID = "txt" + _columnName; container.Controls.Add(txt); break; case ListItemType.Header: // 在 HeaderTemplate 中添加控件 Label headerLbl = new Label(); headerLbl.Text = _columnName; container.Controls.Add(headerLbl); break; } } } ``` 在上述代码中,我们首先在 Page_Load 事件中创建了一个 TemplateField 对象,然后设置了该的 HeaderText 和 ItemTemplate。这里的 ItemTemplate 是通过一个自定义的 GridViewTemplate 类实现的,该类实现了 ITemplate 接口,根据 ListItemType 的不同来实现不同的模板。最后,我们将创建的添加到 GridView 控件的 Columns 集合中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值