Devexpress Gridview 自定义汇总CustomSummaryCalculate(加权平均)
Devexpress Gridview 提供了简单的求和,平均等方法,复杂的汇总方法则需要自定义,使用gridview 的CustomSummaryCalculate 事件,根据官网的文档及各论坛案例实现加权平均的方法。
gridView1.CustomSummaryCalculate += new CustomSummaryEventHandler(view_CustomSummaryCalculate);
自定义汇总方法(加权平均)
计算公式:若n个数
的权分别是
,那么

叫做这n个数的加权平均值。
private void gridView1_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e)
{
if (e.Item != null)
{
//if (e.IsGroupSummary) {} 分组汇总
//if (e.IsTotalSummary) {} 全部汇总 var gridView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
if (gridView.Columns.ColumnByFieldName(WeightColumn) != null)
{
GridSummaryItem gridSummaryItem = e.Item as DevExpress.XtraGrid.GridSummaryItem;
switch (e.SummaryProcess)
{
//calculation entry point
case DevExpress.Data.CustomSummaryProcess.Start:
customSummaryValue = ;
sumWt = ;
break;
//consequent calculations
case CustomSummaryProcess.Calculate:
if (e.FieldValue != null && e.FieldValue != DBNull.Value)
{
sumWt += Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, WeightColumn));
customSummaryValue += Convert.ToDecimal(e.FieldValue) * Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, WeightColumn));
}
break;
//final summary value
case CustomSummaryProcess.Finalize:
e.TotalValue = ((sumWt == ) ? : (customSummaryValue / sumWt));
break;
}
}
}
}
示例图片

示例功能代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.Data; namespace TEST
{
public partial class FormAverage : Form
{
public FormAverage()
{
InitializeComponent();
} private void FormAverage_Load(object sender, EventArgs e)
{
// 显示记录数量
GridColumnSummaryItem idTotal = new GridColumnSummaryItem();
idTotal.SummaryType = SummaryItemType.Count;
idTotal.DisplayFormat = "{0} records"; // 对 Length 字段列 汇总平均
GridColumnSummaryItem lengthAverage = new GridColumnSummaryItem();
lengthAverage.SummaryType = SummaryItemType.Average;
lengthAverage.FieldName = "Length";
lengthAverage.DisplayFormat = "Average: {0:#.#}"; // 汇总项目绑定到相应列,显示在view footer
gridView1.Columns["ID"].SummaryItem.Collection.Add(idTotal);
gridView1.Columns["Length"].SummaryItem.Collection.Add(lengthAverage);
gridView1.OptionsView.ShowFooter = true; DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Length", typeof(int));
dt.Columns.Add("Price", typeof(decimal));
dt.Columns.Add("Num", typeof(int));
dt.Rows.Add(, , , );
dt.Rows.Add(, , , );
dt.Rows.Add(, , , );
dt.Rows.Add(, , , );
this.gridControl1.DataSource = dt;
this.gridColumn1.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Min, "Price", "MIN={0}", "Min"),
new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count, "Price", "Count = {0}", "Count"),
new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, "Price", "Custom Summary = {0}")});
CalacWeightAverage(gridView1, "Price", "Length","Num");
} Decimal sumWt = ; //权重和
Decimal customSummaryValue;
string WeightColumn; //权重列 //计算加权平均
void CalacWeightAverage(DevExpress.XtraGrid.Views.Grid.GridView view, string weightColumn, params string[] weightAverageColumns)
{
WeightColumn = weightColumn;
if (view.Columns.ColumnByFieldName(weightColumn) != null)
{
if ((view.Columns[weightColumn].ColumnType == typeof(decimal))
|| (view.Columns[weightColumn].ColumnType == typeof(int)))
{
for (int i = ; i < weightAverageColumns.Length; i++)
{
string fieldName = weightAverageColumns[i];
if (view.Columns.ColumnByFieldName(fieldName) != null
&& (!(view.Columns[fieldName].ColumnType != typeof(decimal))
|| !(view.Columns[fieldName].ColumnType != typeof(int))))
{
view.Columns[fieldName].SummaryItem.SummaryType = SummaryItemType.Custom;
GridSummaryItem gridSummaryItem = view.GroupSummary.Add(SummaryItemType.Custom, fieldName, view.Columns[fieldName]);
gridSummaryItem.DisplayFormat = view.Columns[fieldName].SummaryItem.DisplayFormat;
}
}
//gridView1.CustomSummaryCalculate += new CustomSummaryEventHandler(gridView1_CustomSummaryCalculate);
}
}
} // 自定义汇总算法
private void gridView1_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e)
{
if (e.Item != null)
{
//if (e.IsGroupSummary) {} 分组汇总
//if (e.IsTotalSummary) {} 全部汇总 var gridView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
if (gridView.Columns.ColumnByFieldName(WeightColumn) != null)
{
GridSummaryItem gridSummaryItem = e.Item as DevExpress.XtraGrid.GridSummaryItem;
switch (e.SummaryProcess)
{
//calculation entry point
case DevExpress.Data.CustomSummaryProcess.Start:
customSummaryValue = ;
sumWt = ;
break;
//consequent calculations
case CustomSummaryProcess.Calculate:
if (e.FieldValue != null && e.FieldValue != DBNull.Value)
{
sumWt += Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, WeightColumn));
customSummaryValue += Convert.ToDecimal(e.FieldValue) * Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, WeightColumn));
}
break;
//final summary value
case CustomSummaryProcess.Finalize:
e.TotalValue = ((sumWt == ) ? : (customSummaryValue / sumWt));
break;
}
}
}
}
}
}
参考资料:
https://documentation.devexpress.com/CoreLibraries/DevExpress.Data.CustomSummaryEventArgs.class
https://www.cnblogs.com/EasyInvoice/p/3892136.html
Devexpress Gridview 自定义汇总CustomSummaryCalculate(加权平均)的更多相关文章
- DevExpress GridView 自定义搜索按钮改为中文内容
首先将 GridControl 控件的搜索功能显示出来. http://www.cnblogs.com/DeepLearing/p/3887601.html 显示效果如下: 可以通过 GridLoca ...
- DevExpress gridview下拉框的再次研究
原文:DevExpress gridview下拉框的再次研究 前几天写了一篇关于研究DevExpress gridview下拉框的随笔(DevExpress gridview下拉框repository ...
- DevExpress GridView 整理(转)
DevExpress GridView 那些事儿 1:去除 GridView 头上的 "Drag a column header here to group by that column&q ...
- DevExpress GridView 那些事儿
1:去除 GridView 头上的 "Drag a column header here to group by that column" --> 点击 Run Desig ...
- DevExpress GridView 整理
1:去除 GridView 头上的 "Drag a column header here to group by that column" --> 点击 Run Desig ...
- DevExpress GridView.CustomSummaryCalculate 实现自定义Group Summary
--首发于博客园, 转载请保留链接 博客原文 DevExpress Documentation官方地址:GridView.CustomSummaryCalculate Event 1. 概要 界面上 ...
- DevExpress中GridControl自定义汇总列值(有选择性的汇总)
今天碰到有同事遇到这个方面的需求,贴一下吧. private void gvTop_CustomSummaryCalculate(object sender, CustomSummaryEventAr ...
- DevExpress GridView属性说明
转自http://www.cnblogs.com/-ShiL/archive/2012/06/08/ShiL201206081335.html (一)双击展开,收缩字表 1 Private Sub E ...
- Devexpress GridView 列中显示图片
首先将图片添加到ImageList中 添加GridView中Column void gridView1_CustomUnboundColumnData(object sender, DevExpres ...
随机推荐
- 转载:VS项目属性配置总结
本文来自:http://www.mamicode.com/info-detail-232474.html https://www.cnblogs.com/alinh/p/8066820.h ...
- 四、Java web 部 分试题
1 .Tomcat 的 优 化 经 验 答:去掉对 web.xml 的监视,把 jsp 提前编辑成 Servlet. 有富余物理内存的情况,加大 tomcat 使用的 jvm 的内存 2 .HTTP ...
- nginx 服务器常见配置以及负载均衡
# 配置启动用户,用户权限不够会出现访问 403 的情况 user root; # 启动多少个工作进程 worker_processes 1; # 错误日志文件进程文件的保存地址 error_log ...
- 软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序
软件工程第三个程序:“WC项目” —— 文件信息统计(Word Count ) 命令行程序 格式:wc.exe [parameter][filename] 在[parameter]中,用户通过输入参数 ...
- How to start a VirtualBox VM headless in Windows 10
If you wanted to start a VirtualBox VM headless (no UI) in the past, you needed additional tools. I ...
- MySQL5.7 并行复制配置
转自:https://www.cnblogs.com/langdashu/p/6125621.html [MySQL] 号称永久解决了复制延迟问题的并行复制,MySQL5.7 一.缘由: 某天看到主从 ...
- pbft流程深层分析和解释(转)
<1>pbft五阶段请求解释 Request pre-prepare prepare commit 执行并reply (1)pre-prepare阶段: 主节点收到客户端请求, ...
- C语言复习: 二级指针和多级指针
二级指针内存模型建立 void main2() { int i = 0; //指针数组 char * p1[] = { "123", "456 ...
- while read line
# grep "请求报文:" application-20170822-*.log >> applog # cat applog|cut -d ":" ...
- 虚拟机安装VMware tools
选择虚拟机菜单栏--安装VMware tools 2 然后在CentOS系统中弹出的VMware tools窗口中 右击VMwaretools-9.6.0-1294478.tar.gz 解压缩到 3 ...