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 ...
随机推荐
- python3自动生成并运行bat批处理,并重定向输入消除黑窗口
#coding:utf-8import os #bat文件的内容(temp.bat)bat_name='temp.bat's1='''echo offipconfigecho Hello world! ...
- kong API gateway
参考:https://www.cnblogs.com/chenjinxi/p/8724564.html 一.简介 Kong,是由Mashape公司开源的,基于Nginx的API gateway. 二. ...
- idea 添加代码自动提示支持,已PHP扩展 swoole 为例
1,下载代码支持包 => swoole-ide-helper-en => https://github.com/eaglewu/swoole-ide-helper.git 2,如果安装了 ...
- java.lang.IllegalMonitorStateException异常
转自:https://blog.csdn.net/qianshangding0708/article/details/48290937
- webvtt字幕转srt字幕的python程序(附改名程序)
最近写了两个比较简单的python程序,原有都是由于看公开课感觉比较费劲,一个是下载的视频无用的名字太长,另一个就是下载的vtt字幕播放器不识别,写了一个vtt转换成str字幕格式的文件 vtt to ...
- SSL证书没有绿锁您与此网站建立的连接并非完全安全解决办法
为什么我新建的网站配置好SSL后,网站https旁边提示不安全,没有小绿锁了? 不少国内空间的新手站长,当使用了SSL证书之后,发现浏览器有https效果了,但是没有绿锁,谷歌浏览器提示“您与此网站建 ...
- 【367】通过 python 实现 SVM 硬边界 算法
参考: 支持向量机整理 SVM 硬边界的结果如下: $$min \quad \frac{1}{2} \sum_{i=1}^m\sum_{j=1}^m \alpha_i\alpha_jy_iy_j \v ...
- 第三条博客 你好 Java web!
今天周五了,明天就是周末!第一个周末就这样结束了 今天我学习了MyEclipse开发 Javaweb 程序,学的是最基础的那一部分 首先先创建了自己的项目,展开项目标签,编辑index.jsp. 我还 ...
- C++ MFC常用函数(转)
WinExec() ExitWindowsEx() GlobalMemoryStatus() GetSystemInfo() GetSystemDirectory() GetWindowsDirect ...
- C++学习一explicit
explicit关键字 C++中的关键字explicit主要是用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换.类构造函数默认情况下声明为隐式的即i ...