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/WindowsForms/701/Controls-and-Libraries/Data-Grid/Summaries/Working-with-Summaries-in-Code

https://documentation.devexpress.com/CoreLibraries/DevExpress.Data.CustomSummaryEventArgs.class

https://www.cnblogs.com/EasyInvoice/p/3892136.html

Devexpress Gridview 自定义汇总CustomSummaryCalculate(加权平均)的更多相关文章

  1. DevExpress GridView 自定义搜索按钮改为中文内容

    首先将 GridControl 控件的搜索功能显示出来. http://www.cnblogs.com/DeepLearing/p/3887601.html 显示效果如下: 可以通过 GridLoca ...

  2. DevExpress gridview下拉框的再次研究

    原文:DevExpress gridview下拉框的再次研究 前几天写了一篇关于研究DevExpress gridview下拉框的随笔(DevExpress gridview下拉框repository ...

  3. DevExpress GridView 整理(转)

    DevExpress GridView 那些事儿 1:去除 GridView 头上的 "Drag a column header here to group by that column&q ...

  4. DevExpress GridView 那些事儿

    1:去除 GridView 头上的 "Drag a column header here to group by that column" -->  点击 Run Desig ...

  5. DevExpress GridView 整理

    1:去除 GridView 头上的 "Drag a column header here to group by that column" -->  点击 Run Desig ...

  6. DevExpress GridView.CustomSummaryCalculate 实现自定义Group Summary

    --首发于博客园, 转载请保留链接  博客原文 DevExpress Documentation官方地址:GridView.CustomSummaryCalculate Event 1. 概要 界面上 ...

  7. DevExpress中GridControl自定义汇总列值(有选择性的汇总)

    今天碰到有同事遇到这个方面的需求,贴一下吧. private void gvTop_CustomSummaryCalculate(object sender, CustomSummaryEventAr ...

  8. DevExpress GridView属性说明

    转自http://www.cnblogs.com/-ShiL/archive/2012/06/08/ShiL201206081335.html (一)双击展开,收缩字表 1 Private Sub E ...

  9. Devexpress GridView 列中显示图片

    首先将图片添加到ImageList中 添加GridView中Column void gridView1_CustomUnboundColumnData(object sender, DevExpres ...

随机推荐

  1. Mysql 视图使用

    视图 简单理解视图就是一张虚拟表,可以简化一些复杂查询语句 举个简单的例子来理解视图 视图是虚拟的表,与包含数据的表不一样,视图只包含使用时动态检索数据的查询:不包含任何列或数据.使用视图可以简化复杂 ...

  2. Spring和SpringMVC的常用注解

    Spring的部分: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component-scan base-package=" ...

  3. Vim 常用命令和编辑方法

    命令模式 :e <path/to/file> → 打开一个文件 :w → 存盘 :wq → 存盘 + 退出 (:w 存盘, :q 退出)   (陈皓注::w 后可以跟文件名) :savea ...

  4. python语言中的数据类型之集合

    数据类型 集合类型    set 用途:1.关系运算        2.去重 定义方式:在{}内用逗号分隔开多个元素,但元素的特点是 1.集合内元素必须是不可变类型 2.集合内元素无序 集合内元素不能 ...

  5. Java相关文章

    servlet/filter/listener/interceptor区别与联系 web.xml配置详解 在Eclipse中使用JUnit4进行单元测试(初级篇) 单点登录原理与简单实现 spring ...

  6. UNITY优化资料收集

    U3D手册: Optimizing garbage collection in Unity games https://zhuanlan.zhihu.com/p/25306993 https://gi ...

  7. How to Pronounce the Numbers 1 – 10

    How to Pronounce the Numbers 1 – 10 Share Tweet Share Tagged With: Numbers Numbers are something you ...

  8. centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod

    centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod ...

  9. linux网卡桥接问题与docker网卡桥接问题

    一.linux网卡桥接问题 在linux上创建桥接网卡,与真实的物理网卡进行绑定,相当于在linux中创建了一个虚拟的交换机,以linux网卡地址为源地址的数据,从桥接网卡br0进入,从实际的物理网卡 ...

  10. C# 无法识别的属性“targetFramework”。请注意属性名称区分大小写。错误解决办法

    “/CRM”应用程序中的服务器错误. 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 无法识别的属性“targetF ...