DevExpress GridControl 实现自定义汇总

注册事件

1
gvDetail.CustomSummaryCalculate += GvDetail_CustomSummaryCalculate; //实现自定义汇总

设置汇总列

1
2
3
4
5
6
7
8
#region 自定义汇总

gvDetail.OptionsView.ShowFooter = true;
gvDetail.Columns["Quantity"].SummaryItem.SummaryType = SummaryItemType.Custom; //到这里会触发事件
gvDetail.Columns["Quantity"].SummaryItem.DisplayFormat = "特殊合计:{0}";
gvDetail.UpdateSummary(); //因为先绑定了数据又设置了汇总,可以手动触发一次计算

#endregion

汇总事件(方法一):直接用数据源dt计算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// <summary>
/// 自定义汇总
/// </summary>
private void GvDetail_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e)
{
//只有当GridSummaryItem的类型为Custom时,才会触发事件
GridSummaryItem gridSummaryItem = e.Item as GridSummaryItem;
if (gridSummaryItem.FieldName == "Quantity")
{

//第1阶段:开始
if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Start){
e.TotalValueReady = true; //设置此项可直接进入Finalize,跳过耗时的Calculate
}

//第2阶段:计算中,会遍历所有行
if (e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Calculate)
{
//因为在Start设置了e.TotalValueReady = true; 所以这里不会执行
}


//在Finalize进行赋值,也就是循环到最后一行时通过对背后的数据源进行计算一次性赋值
if (e.SummaryProcess == CustomSummaryProcess.Finalize)
{
//取出数据源,可能已过滤
var filteredRows = gvDetail.DataController.GetAllFilteredAndSortedRows();

//通过Linq进行筛选计算
var query = from r in filteredRows.Cast<DataRowView>()
where !r.Row["ProductFullName"].ToString().Contains("日薪") && !r.Row["ProductFullName"].ToString().Contains("QT-") //日薪和QT不统计
select r;

var result = query.Sum(c => float.Parse(c.Row["Quantity"] + ""));

e.TotalValue = result;
}
}
}

汇总事件(方法二):逐行遍历累计

需求

使用DevExpress GridControl 时,有一栏为colSelected(FieldName 为”Selected”) ,需根据此栏是否选中来计算另外一栏colCurrentApplyQuantity(FieldName 为”CurrentApplyQuantity”)的Summary.

依据默认的SummaryItem是无法实现的,需要使用Custom Aggregate Functions. 具体处理过程如下:

实现

  1. 设置Gridview OptionsView ShowFooter 为True,用来显示最终的Summary。
  2. 设置colCurrentApplyQuantity 的SummaryType为Custome,Tag为1,Tag是用来区分SummaryItem,如需要格式化显示的结果,可设置相应的DisplayFormat。
  3. 使用gridView1_CustomSummaryCalculate事件来实现依据colSelected来colCurrentApplyQuantity的Summary。
  4. 求和包括3个阶段,Initialization, Calculation 和Finalization。依据 CustomSummaryEventArgs.SummaryProcess的值来决定当前进行的那个阶段。具体实现如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//定义汇总存放变量
int customSum;

/// <summary>
/// 自定义汇总事件
/// </summary>
private void gridView1_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e)
{
int summaryID = Convert.ToInt32((e.Item as GridSummaryItem).Tag);
GridView gridView = sender as GridView;

//初始化阶段,汇总值清零
if (e.SummaryProcess == CustomSummaryProcess.Start)
{
customSum = 0;
}

//计算阶段,会便利所有行,当前行为e.RowHandle
if (e.SummaryProcess == CustomSummaryProcess.Calculate)
{
bool isSelected = (bool)gridView.GetRowCellValue(e.RowHandle, "Selected");
switch (summaryID)
{
case 1: // The total summary calculated against the 'CurrentApplyQuantity' column.
if (isSelected) customSum += Convert.ToInt32(e.FieldValue);
break;
}
}

//计算完成,赋值
if (e.SummaryProcess == CustomSummaryProcess.Finalize)
{
switch (summaryID)
{
case 1:
e.TotalValue = customSum;
break;
}
}
}