Devexpress Gridview 提供了簡單的求和,平均等方法,複雜的彙總方法則需要自定義,使用gridview 的CustomSummaryCalculate 事件,根據官網的文檔及各論壇案例實現加權平均的方法。 自定義彙總方法(加權平均) 參考資料: https://documentati ...
Devexpress Gridview 提供了簡單的求和,平均等方法,複雜的彙總方法則需要自定義,使用gridview 的CustomSummaryCalculate 事件,根據官網的文檔及各論壇案例實現加權平均的方法。
gridView1.CustomSummaryCalculate += new CustomSummaryEventHandler(view_CustomSummaryCalculate);
自定義彙總方法(加權平均)
3 // 顯示記錄數量 4 GridColumnSummaryItem siTotal = new GridColumnSummaryItem(); 5 siTotal.SummaryType = SummaryItemType.Count; 6 siTotal.DisplayFormat = "{0} records"; 7 8 // 對 Length 欄位列 彙總平均 9 GridColumnSummaryItem siAverage = new GridColumnSummaryItem(); 10 siAverage.SummaryType = SummaryItemType.Average; 11 siAverage.FieldName = "Length"; 12 siAverage.DisplayFormat = "Average: {0:#.#}"; 14 15 // gridView1中colIdRandom 列顯示多行彙總:最小、計數、加權平均 16 this.colIdRandom.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] { 17 new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Min, "IdRandom", "MIN={0}", "Min"), 18 new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count, "IdRandom", "Count = {0}", "Count"), 19 new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, "IdRandom", "Custom Summary = {0}")}); 20 // 計算加權平均 21 CalacWeightAverage(gridView1,"WeightColumn","TEST1",colIdRandom.FieldName); 22 23 // 彙總項目綁定到相應列,顯示在view footer 24 gridView1.Columns["ID"].SummaryItem.Collection.Add(siTotal); 25 gridView1.Columns["Length"].SummaryItem.Collection.Add(siAverage); 26 gridView1.OptionsView.ShowFooter = true; 27 28 29 double sumWt=0; 30 double customSummaryValue; 31 string WeightColumn; 32 33 void CalacWeightAverage(DevExpress.XtraGrid.Views.Grid.GridView view, string weightColumn, params string[] weightAverageColumns) 34 { 35 WeightColumn = weightColumn; 36 if (view.Columns.ColumnByFieldName(weightColumn) != null) 37 { 38 if (!(view.Columns[weightColumn].ColumnType != typeof(decimal)) 39 || !(view.Columns[weightColumn].ColumnType != typeof(int))) 40 { 41 for (int i = 0; i < weightAverageColumns.Length; i++) 42 { 43 string fieldName = weightAverageColumns[i]; 44 if (view.Columns.ColumnByFieldName(fieldName) != null 45 && (!(view.Columns[fieldName].ColumnType != typeof(decimal)) 46 || !(view.Columns[fieldName].ColumnType != typeof(int)))) 47 { 48 view.Columns[fieldName].SummaryItem.SummaryType = SummaryItemType.Custom; 49 GridSummaryItem gridSummaryItem = view.GroupSummary.Add(SummaryItemType.Custom, fieldName, view.Columns[fieldName]); 50 gridSummaryItem.DisplayFormat = view.Columns[fieldName].SummaryItem.DisplayFormat; 51 } 52 } 53 //gridView1.CustomSummaryCalculate += new CustomSummaryEventHandler(gridView1_CustomSummaryCalculate); 54 } 55 } 56 } 57
// 自定義彙總演算法 58 void gridView1_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e) 59 { 60 if (e.Item != null) 61 { 62 //if (e.IsGroupSummary) {} 分組彙總 63 //if (e.IsTotalSummary) {} 全部彙總 64 65 var gridView = sender as DevExpress.XtraGrid.Views.Grid.GridView; 66 if (gridView.Columns.ColumnByFieldName(WeightColumn) != null) 67 { 68 GridSummaryItem gridSummaryItem = e.Item as GridSummaryItem; 69 if (e.SummaryProcess == CustomSummaryProcess.Start) 70 { 71 customSummaryValue = 0; 72 sumWt = 0; 73 } 74 else if (e.SummaryProcess == CustomSummaryProcess.Calculate) 75 { 76 if (e.FieldValue != null && e.FieldValue != DBNull.Value) 77 { 78 sumWt += Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, WeightColumn)); 79 customSummaryValue += Convert.ToDecimal(e.FieldValue) * Convert.ToDecimal(gridView.GetRowCellValue(e.RowHandle, WeightColumn)); 80 } 81 } 82 else if (e.SummaryProcess == CustomSummaryProcess.Finalize) 83 { 84 e.TotalValue = ((sumWt == 0) ? 0 : (customSummaryValue / sumWt)); 85 } 86 } 87 } 88 }
參考資料:
https://documentation.devexpress.com/CoreLibraries/DevExpress.Data.CustomSummaryEventArgs.class
https://www.cnblogs.com/EasyInvoice/p/3892136.html