-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecalculateGridTotalsModule.cs
84 lines (78 loc) · 4.4 KB
/
RecalculateGridTotalsModule.cs
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using DevExpress.DashboardCommon;
using DevExpress.DashboardExport;
using DevExpress.DashboardWin;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;
using System.Linq;
namespace WinFormsDashboard_RecalculateTotals {
public class RecalculateGridTotalsModule {
IDashboardControl dashboardControl;
public void Attach(IDashboardControl dashboardControl) {
if (dashboardControl != null) {
this.dashboardControl = dashboardControl;
this.dashboardControl.DashboardItemControlUpdated += DashboardControl_DashboardItemControlUpdated;
this.dashboardControl.CustomExport += DashboardControl_CustomExport;
}
}
public void Detach() {
if (dashboardControl != null) {
this.dashboardControl.DashboardItemControlUpdated -= DashboardControl_DashboardItemControlUpdated;
this.dashboardControl.CustomExport -= DashboardControl_CustomExport;
}
}
void DashboardControl_DashboardItemControlUpdated(object sender, DashboardItemControlEventArgs e) {
if (e.GridControl != null) {
var gridDashboardItem = (GridDashboardItem)dashboardControl.Dashboard.Items[e.DashboardItemName];
var gridView = (GridView)e.GridControl.MainView;
var gridContext = e.GridContext;
RecalculateGridSummary(gridContext, gridDashboardItem, gridView);
gridView.ColumnFilterChanged += (s, args) => {
RecalculateGridSummary(gridContext, gridDashboardItem, gridView);
};
}
}
void DashboardControl_CustomExport(object sender, DevExpress.DashboardCommon.CustomExportEventArgs e) {
var detailBand = e.Report.Bands[BandKind.Detail];
var gridFooters = detailBand.Controls.OfType<XRGridFooterPanel>();
foreach (var gridFooter in gridFooters) {
var gridControl = (GridControl)dashboardControl.GetUnderlyingControl(gridFooter.GridComponentName);
var gridView = (GridView)gridControl.MainView;
foreach (GridColumn column in gridView.Columns) {
var dataItemName = column.FieldName;
var columnTotals = gridFooter.GetColumnTotals(dataItemName);
for (int i = 0; i < columnTotals.Count; i++) {
columnTotals[i].Text = column.Summary[i].DisplayFormat;
}
}
}
}
void RecalculateGridSummary(GridContext gridContext, GridDashboardItem gridItem, GridView underlyingGridView) {
var itemData = dashboardControl.GetItemData(gridItem.ComponentName);
foreach (GridColumn column in underlyingGridView.Columns) {
var gridDashboardColumn = gridContext.GetDashboardItemColumn(gridItem, column);
if (gridDashboardColumn != null) {
var dataItemName = column.FieldName;
object dataItemDescriptor = null;
dataItemDescriptor = itemData.GetMeasures().FirstOrDefault(m => m.ID == dataItemName) ?? dataItemDescriptor;
dataItemDescriptor = itemData.GetDimensions(DashboardDataAxisNames.DefaultAxis).FirstOrDefault(d => d.ID == dataItemName) ?? dataItemDescriptor;
if (dataItemDescriptor != null) {
UpdateColumnSummaryItemText(gridDashboardColumn, column, dataItemDescriptor);
}
}
}
}
void UpdateColumnSummaryItemText(GridColumnBase gridDashboardColumn, GridColumn gridcolumn, object dataItemDescriptor) {
gridcolumn.Summary.Clear();
foreach (GridColumnTotal total in gridDashboardColumn.Totals) {
var gridSummaryItem = new GridColumnSummaryItem();
gridcolumn.Summary.Add(gridSummaryItem);
gridSummaryItem.SummaryType = DashboardTotalsHelper.GetGridColumnTotalType(total.TotalType);
gridSummaryItem.DisplayFormat = total.TotalType != GridColumnTotalType.Auto ?
string.Format("{0} = {1}", DashboardTotalsHelper.GetTotalPrefix(total.TotalType), FormatHelper.Format(dataItemDescriptor, gridSummaryItem.SummaryValue)) :
"Auto: Not Supported";
}
}
}
}