How To Hide Subtotals in the PivotGridControl Programmatically
To Show/Hide the subtotals we can normally make use of ShowSubTotal property value. By setting the property value to true or false, we can show/Hide subtotals value.
This can also be achieved by programmatically without the property value incase if u don’t use that property. The following program explains the above mentioned issue.
Code Snippet to Hide the Row subtotals:
C#
int row, column; PivotCellInfo cellInfo = null; if (this.pivotGrid1.PivotEngine != null) { for (row = this.pivotGrid1.PivotEngine.PivotColumns.Count + (!this.pivotGrid1.PivotEngine.ShowCalculationsAsColumns ? 1 : 0); row < this.pivotGrid1.PivotEngine.RowCount; row++) { for (column = 0; column < this.pivotGrid1.PivotEngine.PivotRows.Count - 1; column++) { cellInfo = this.pivotGrid1.PivotEngine[row, column]; if (cellInfo != null) { if (cellInfo.CellType.ToString().Contains("TotalCell") && !cellInfo.CellType.ToString().Contains("Grand")) { if (this.ShowSubTotal.IsChecked.HasValue) if(!this.ShowSubTotal.IsChecked.Value) this.pivotGrid1.InternalGrid.Model.RowHeights.SetHidden(row, row, true); //Specified Row will be hidden else this.pivotGrid1.InternalGrid.Model.RowHeights.SetHidden(row, row, false); ////Specified Row will be Shown } } } } this.pivotGrid1.InternalGrid.InvalidateCells(); }
Figure: PivotGrid shows Row subtotals hidden
Code Snippet to Hide Column Subtotals:
int row, column; PivotCellInfo cellInfo = null; for (column = this.pivotGrid1.PivotEngine.PivotRows.Count; column < this.pivotGrid1.PivotEngine.ColumnCount; column++) { for (row = 0; row < this.pivotGrid1.PivotEngine.PivotColumns.Count - 1; row++) { cellInfo = this.pivotGrid1.PivotEngine[row, column]; if (cellInfo != null) { if (cellInfo.CellType.ToString().Contains("TotalCell") && !cellInfo.CellType.ToString().Contains("Grand")) { if (this.ShowColumnTotal.IsChecked.HasValue ) if( !this.ShowColumnTotal.IsChecked.Value) this.pivotGrid1.InternalGrid.Model.ColumnWidths.SetHidden(column, column + this.pivotGrid1.PivotCalculations.Count, true); //Specified Column will be hidden else this.pivotGrid1.InternalGrid.Model.ColumnWidths.SetHidden(column, column + this.pivotGrid1.PivotCalculations.Count, false); //Specified Column will be shown } } } } this.pivotGrid1.InternalGrid.InvalidateCells(); }
|
Figure: PivotGrid shows column subtotals hidden
Code Snippet to Hide the Row and Column Subtotals at the Initial Loading:
C#
public MainWindow() { InitializeComponent(); this.pivotGrid1.Loaded += (s, a) => { this.pivotGrid1.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new Action(() => { HideSubtotals(); })); }; } void HideSubtotals() { int row, column; PivotCellInfo cellInfo = null; if (this.pivotGrid1.PivotEngine != null) { for (row = this.pivotGrid1.PivotEngine.PivotColumns.Count + (!this.pivotGrid1.PivotEngine.ShowCalculationsAsColumns ? 1 : 0); row < this.pivotGrid1.PivotEngine.RowCount; row++) { for (column = 0; column < this.pivotGrid1.PivotEngine.PivotRows.Count - 1; column++) { cellInfo = this.pivotGrid1.PivotEngine[row, column]; if (cellInfo != null) { if (cellInfo.CellType.ToString().Contains("TotalCell") && !cellInfo.CellType.ToString().Contains("Grand")) { this.pivotGrid1.InternalGrid.Model.RowHeights.SetHidden(row, row, true); } } } } for (column = this.pivotGrid1.PivotEngine.PivotRows.Count; column < this.pivotGrid1.PivotEngine.ColumnCount; column++) { for (row = 0; row < this.pivotGrid1.PivotEngine.PivotColumns.Count - 1; row++) { cellInfo = this.pivotGrid1.PivotEngine[row, column]; if (cellInfo != null) { if (cellInfo.CellType.ToString().Contains("TotalCell") && !cellInfo.CellType.ToString().Contains("Grand")) { this.pivotGrid1.InternalGrid.Model.ColumnWidths.SetHidden(column, column + this.pivotGrid1.PivotCalculations.Count, true); } } } }
|