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 ...
随机推荐
-
简单的mysql查询
mysql是基于客户机-服务器的数据库.客户机-服务器应用分为两个不同的部分.服务器部分是负责所有数据访问和处理的一个软件. 连接mysql 要连接mysql需要知道如下 主机名: 本地为localh ...
-
Segment Tree 分类: ACM TYPE 2014-08-29 13:04 97人阅读 评论(0) 收藏
#include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int ...
-
DTcms 扩展字段标签调用
前台模版: 文章列表:{dr[author]} 文章内容{model.fields[author]} 点击数 后台CS文件:model.fields["author"].ToStr ...
-
Java基础知识强化之IO流笔记25:FileInputStream / FileOutputStream 复制图片案例
1. 需求:把D:\\美女.jpg 复制到当前项目目录下mn.jpg 代码示例: package com.himi.filecopy; import java.io.FileInputStream; ...
-
myql_链接丢失异常_mybaits _等框架_报错_The last packet successfully
mysql 8小时问题的解决方法 转发: 别看是英文 ,写的很好 ,才转 Use Hibernate + MYSQL database development, link timeout proble ...
-
dbcp的配置
tomcat的 配置,进入conf->context.xml <Resource name="mysql" auth="Container" ...
-
解决Yii2邮件发送问题(结果返回成功,但接收不到邮件)
刚刚用了一下yii邮件发送功能,虽然结果返回成功,但接收不到邮件.配置文件代码如下: 'components' => [ 'db' => [ 'class' => 'yii\db\C ...
-
2017-2018-1 20155214&;20155216 实验四:外设驱动程序设计
2017-2018-1 20155214&20155216 实验四:外设驱动程序设计 实验四外设驱动程序设计-1 实验要求: 学习资源中全课中的"hqyj.嵌入式Linux应用程序开 ...
- 百度BAE的使用
-
菜鸟从零学编程——GET与POST
相信大家在面试的时候经常会被问到:GET与POST有什么区别吧?你是怎么回答的呢?POST比GEt安全?GET有URL的长度限制而POST没有或者很大?GET通过URL或者Cookie传参数,POST ...