Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.
A template that defines the presentation of field headers when the pivot grid is printed is specified by the PivotGridControl.PrintFieldHeaderTemplate property. If you have more than one template that can be used to render field headers, you can implement custom logic to choose the required template. To do this, derive from the DataTemplateSelector class, implement the SelectTemplate method that returns a template which meets the required condition, and assign an instance of this class to the PrintFieldHeaderTemplateSelector property.
This example demonstrates how to select print templates for the DXPivotGrid elements based on a custom logic.
The logic for selecting the data cell print template is based on the ratio of the data cell value to the grand total value. If the ratio is between 0.2 and 0.8, the template selector applies a template that highlights the cell text and adds the warning sign to the cell.
usingSystem;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingDevExpress.Xpf.PivotGrid;
usingDevExpress.Xpf.Printing;
namespaceDXPivotGrid_SelectingPrintTemplate {
publicpartialclassMainWindow : Window {
publicMainWindow() {
InitializeComponent();
picotGridControl1.DataSource =
new nwindDataSetTableAdapters.SalesPersonTableAdapter().GetData();
}
privatevoidbtnPrint_Click(object sender, RoutedEventArgs e) {
PrintHelper.ShowPrintPreview(this, picotGridControl1);
}
}
publicclassCellTemplateSelector : DataTemplateSelector {
publicoverride DataTemplate SelectTemplate(object item, DependencyObject container) {
Window mainWindow = Application.Current.MainWindow;
CellElementData cell = (CellElementData)item;
// Calculate the ratio of the cell value to the grand total.double share = Convert.ToDouble(cell.Value) / Convert.ToDouble(cell.ColumnTotalValue);
// Apply the Normal template to the Column Grand Total cells.if (cell.ColumnValue == null)
return mainWindow.FindResource("NormalCellTemplate") as DataTemplate;
// If the ratio is far from 0.5, use the Highlighted template.// Otherwise, use the Normal template.if (share > 0.8 || share < 0.2)
return mainWindow.FindResource("HighlightedCellTemplate") as DataTemplate;
elsereturn mainWindow.FindResource("NormalCellTemplate") as DataTemplate;
}
}
}
ImportsSystemImportsSystem.WindowsImportsSystem.Windows.ControlsImportsDevExpress.Xpf.PivotGridImportsDevExpress.Xpf.PrintingNamespace DXPivotGrid_SelectingPrintTemplate
PartialPublicClass MainWindow
Inherits Window
PublicSubNew()
InitializeComponent()
picotGridControl1.DataSource = (New nwindDataSetTableAdapters.SalesPersonTableAdapter()).GetData()
EndSubPrivateSub btnPrint_Click(ByVal sender AsObject, ByVal e As RoutedEventArgs)
PrintHelper.ShowPrintPreview(Me, picotGridControl1)
EndSubEndClassPublicClass CellTemplateSelector
Inherits DataTemplateSelector
PublicOverridesFunction SelectTemplate(ByVal item AsObject, ByVal container As DependencyObject) As DataTemplate
'INSTANT VB NOTE: The variable mainWindow was renamed since it may cause conflicts with calls to static members of the user-defined type with this name:Dim mainWindow_Renamed As Window = Application.Current.MainWindow
Dim cell As CellElementData = DirectCast(item, CellElementData)
' Calculates the share of a cell value in the Column Grand Total value.Dim share AsDouble = Convert.ToDouble(cell.Value) / Convert.ToDouble(cell.ColumnTotalValue)
' Applies the Normal template to the Column Grand Total cells.If cell.ColumnValue IsNothingThenReturnTryCast(mainWindow_Renamed.FindResource("NormalCellTemplate"), DataTemplate)
EndIf' If the share is too far from 50%, the Highlighted template is selected.' Otherwise, the Normal template is applied to the cell.If share > 0.8OrElse share < 0.2ThenReturnTryCast(mainWindow_Renamed.FindResource("HighlightedCellTemplate"), DataTemplate)
ElseReturnTryCast(mainWindow_Renamed.FindResource("NormalCellTemplate"), DataTemplate)
EndIfEndFunctionEndClassEndNamespace