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. CentOS6.9 网络设置

    一.临时设置IP地址 ifconfig eth0 192.168.42.119 broadcast 192.168.42.129 netmask 255.255.255.0 二.上述方法只能临时生效, ...

  2. centos6 安装 docker 问题

    参考:https://www.cnblogs.com/cs294639693/p/10164258.html 第一步:删除  参考:https://www.cnblogs.com/liuyanshen ...

  3. tomcat jvm 内存调优 适用于 JDK 6 & 7

    参考:https://blog.csdn.net/m0_37327416/article/details/76185051 1.jvm内存管理机制: 1)堆(Heap)和非堆(Non-heap)内存 ...

  4. leetcode1018

    根据题目的hint,使用单层循环计算: class Solution(object): def prefixesDivBy5(self, A: 'List[int]') -> 'List[boo ...

  5. 使用expect解决shell交互问题

    比如ssh的时候,如果没设置免密登陆,那么就需要输入密码.使用expect可以做成自动应答 1.expect检测和安装 sudo apt-get install tcl tk expect 2.脚本样 ...

  6. package-info.java

    参考文章: http://blog.sina.com.cn/s/blog_93dc666c0101gzlr.html 对于package-info.java我们并不陌生,但又陌生. 在我们每次建立pa ...

  7. 着色器shaders

    着色器(shader)是运行在GPU上的小程序,为图形渲染管线某个特定部分而运行. 着色器也是一种非常独立的程序,它们之间不能相互通信,它们之间唯一沟通只有通过输入输出. GLSL是为图形计算量身定制 ...

  8. vue 源码阅读记录

    0.webpack默认引入的是vue.runtime.common.js,并不是vue.js,功能有略微差别,不影响使用 1.阅读由ts编译后的js: 入口>构造函数 >定义各类方法 &g ...

  9. python正则表达式查找汉字

    使用正则表达是查找汉字之前,要将所有的字符串都转码成utf8 import re string_test = "This is test string 这是测试字符串" strin ...

  10. spark遇到的错误1-内存不足

    原来的代码: JavaRDD<ArticleReply> javaRdd = rdd.flatMap(new FlatMapFunction<String, ArticleReply ...