Devexpress Gridview 自定义汇总CustomSummaryCalculate(加权平均)_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Devexpress Gridview 自定义汇总CustomSummaryCalculate(加权平均)

Devexpress Gridview 自定义汇总CustomSummaryCalculate(加权平均)

 2017/12/5 3:15:11  flysong  程序员俱乐部  我要评论(0)
  • 摘要:DevexpressGridview提供了简单的求和,平均等方法,复杂的汇总方法则需要自定义,使用gridview的CustomSummaryCalculate事件,根据官网的文档及各论坛案例实现加权平均的方法。gridView1.CustomSummaryCalculate+=newCustomSummaryEventHandler(view_CustomSummaryCalculate);自定义汇总方法(加权平均)1doublesumWt=0
  • 标签:view 自定义

        Devexpress Gridview 提供了简单的求和,平均等方法,复杂的汇总方法则需要自定义,使用gridview 的CustomSummaryCalculate 事件,根据官网的文档及各论坛案例实现加权平均的方法。 

gridView1.CustomSummaryCalculate += new CustomSummaryEventHandler(view_CustomSummaryCalculate);

  自定义汇总方法(加权平均)

class="code_img_closed" src="/Upload/Images/2017120503/0015B68B3C38AA5B.gif" alt="">
 1 double sumWt=0;
 2 double customSummaryValue;
 3 string WeightColumn;
 4 
 5 void AddWeightAverage(DevExpress.XtraGrid.Views.Grid.GridView view, string weightColumn, params string[] weightAverageColumns)
 6 {
 7     WeightColumn = weightColumn;
 8     if (view.Columns.ColumnByFieldName(weightColumn) != null)
 9     {
10         if (!(view.Columns[weightColumn].ColumnType != typeof(decimal)) 
11             || !(view.Columns[weightColumn].ColumnType != typeof(int)))
12         {
13             for (int i = 0; i < weightAverageColumns.Length; i++)
14             {
15                 string fieldName = weightAverageColumns[i];
16                 if (view.Columns.ColumnByFieldName(fieldName) != null 
17                     && (!(view.Columns[fieldName].ColumnType != typeof(decimal)) 
18                     || !(view.Columns[fieldName].ColumnType != typeof(int))))
19                 {
20                     view.Columns[fieldName].SummaryItem.SummaryType = SummaryItemType.Custom;
21                     GridSummaryItem gridSummaryItem = view.GroupSummary.Add(SummaryItemType.Custom, fieldName, view.Columns[fieldName]);
22                     gridSummaryItem.DisplayFormat = view.Columns[fieldName].SummaryItem.DisplayFormat;
23                 }
24             }
25             view.CustomSummaryCalculate -= new CustomSummaryEventHandler(view_CustomSummaryCalculate);
26             view.CustomSummaryCalculate += new CustomSummaryEventHandler(view_CustomSummaryCalculate);
27         }
28     }
29 }
30 
31 void view_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e)
32 {            
33     if (e.Item != null)
34     {
35         var gridView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
36         if (gridView.Columns.ColumnByFieldName(WeightColumn) != null)
37         {
38             GridSummaryItem gridSummaryItem = e.Item as GridSummaryItem;
39             if (e.SummaryProcess == CustomSummaryProcess.Start)
40             {
41                 customSummaryValue = 0;
42                 sumWt = 0;
43             }
44             else if (e.SummaryProcess == CustomSummaryProcess.Calculate)
45             {
46                 if (e.FieldValue != null && e.FieldValue != DBNull.Value)
47                 {
48                     sumWt += Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, WeightColumn));
49                     customSummaryValue += Convert.ToDecimal(e.FieldValue) * Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, GridUtility.WeightColumn));
50                 }
51             }
52             else if (e.SummaryProcess == CustomSummaryProcess.Finalize)
53             {
54                 e.TotalValue = ((sumWt == 0) ? 0 : (customSummaryValue / sumWt));
55             }
56         }
57     }
58 }
logs_code_collapse">View Code

参考资料:

https://documentation.devexpress.com/WindowsForms/DevExpress.XtraGrid.Views.Grid.GridView.CustomSummaryCalculate.event

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

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

上一篇: 刘强东:现在还有几千万人极度贫困是富人的耻辱 下一篇: 没有下一篇了!
发表评论
用户名: 匿名