windowsforms-1883-controls-and-libraries-pivot-grid-data-analysis-conditional-formatting.md
The Pivot Grid control includes a Microsoft Excel-inspired conditional formatting feature that allows you to change the appearance of individual cells based on specific conditions. This feature helps to highlight important information, identify trends and exceptions, and compare data using the collection of format rules.
You can add, remove or edit format rules in different ways: using the PivotGrid Designer at design time, using API or at runtime. You can easily export the result of conditional formatting to a file in various formats (HTML, MHT, PDF, RTF, TXT, CSV, XLS or XLSX).
Tip
Demo : Format Rules module in the XtraPivotGrid MainDemo
Requires installation of WinForms Subscription. Download.
To create a new formatting rule at design time, invoke the PivotGrid Designer and go to the Format Rules page in the Appearances section.
Use the button to add a new rule to the collection. To delete the rule, select it and click the button.
Set the PivotGridFormatRule.Measure property that defines the data field to whose values the format rule is applied.
Select one of the PivotGridFormatRule.Settings property values that specify which data cell/field intersections are involved in conditional formatting. You can select an intersection of row and column fields or specify the type of cells.
Select the FormatRuleBase.Rule property value that specifies the type of a format rule and defines condition and appearance settings applied to data cells.
Set the appearance of formatted cells. You can do this in two ways.
The image below demonstrates a configured rule.
To check whether the format rule is correct, use the PivotGridFormatRule.IsValid property. If this property returns true , the format rule is applied to Pivot Grid data cells.
To disable the format rule, set the FormatRuleBase.Enabled property to false.
You can use the FormatRuleBase.Description property to specify a text description for the created format rule.
There are two general ways of applying formatting rules to the Pivot Grid control at runtime: using the Format Rules Menu or the Conditional Formatting Manager.
Format Rules context menu
If the PivotGridOptionsMenu.EnableFormatRulesMenu property value is set to true , end-users can invoke the Format Rules context menu and select a required rule and predefined style from the list.
Important
To enable the multi-column item layout at the third menu level shown in the image above, add the BarManager component to the form. Otherwise, menu elements at the third menu level will be arranged in a linear list; specific menu items will be displayed without glyphs.
Conditional Formatting Rules Manager
End-users can create, sort and modify the created rules using the Conditional Formatting Rules Manager. To allow end-users to use the Conditional Formatting Rules Manager, ensure that the control’s PivotGridOptionsMenu.EnableFormatRulesMenu property is set to true.
To invoke this manager at runtime, select the Manage Rules… item from the Format Rules context menu.
The Pivot Grid control allows you to apply conditional formatting to data cells or field value cells. By default, an empty collection of style format rules is created with a new instance of the PivotGridControl. The PivotGridControl.FormatRules property provides access to a collection of PivotGridFormatRule objects that are used to define formatting settings.
To add a new format rule, create the PivotGridFormatRule object and specify the following settings.
Finally, add the created format rule to the PivotGridControl.FormatRules collection.
See the list below for information on conditional formatting limitations.
The following example shows how to apply a data bar format condition (the FormatConditionRuleDataBar class) to the Pivot Grid data cells. The Pivot Grid paints cells placed at the intersection of the Year column and the Sales Person row with the yellow gradient. A longer bar corresponds to a higher value, and a shorter bar corresponds to a lower value.
The image below shows the resulting UI.
using System.Windows.Forms;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraEditors;
namespace WinFormsPivotGridFormatRules {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
salesPersonTableAdapter1.Fill(nwindDataSet1.SalesPerson);
// Creates a new FormatRule object.
PivotGridFormatRule newRule = new PivotGridFormatRule();
// Sets a Measure.
newRule.Measure = fieldExtendedPrice;
// Creates and specifies a new Settings object.
newRule.Settings = new FormatRuleFieldIntersectionSettings{
Column = fieldOrderYear1,
Row = fieldSalesPerson
};
// Creates a new Rule object and sets its parameters.
newRule.Rule = new FormatConditionRuleDataBar{
PredefinedName = "Yellow Gradient"
};
// Adds the rule to the collection.
pivotGridControl1.FormatRules.Add(newRule);
}
}
}
Imports System.Windows.Forms
Imports DevExpress.XtraPivotGrid
Imports DevExpress.XtraEditors
Namespace WinFormsPivotGridFormatRules
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
salesPersonTableAdapter1.Fill(nwindDataSet1.SalesPerson)
' Creates a new FormatRule object.
Dim newRule As PivotGridFormatRule = New PivotGridFormatRule()
' Sets a Measure.
newRule.Measure = fieldExtendedPrice
' Creates and specifies a new Settings object.
newRule.Settings = New FormatRuleFieldIntersectionSettings With {.Column = fieldOrderYear1, .Row = fieldSalesPerson}
' Creates a new Rule object and sets its parameters.
newRule.Rule = New FormatConditionRuleDataBar With {.PredefinedName = "Yellow Gradient"}
' Adds the rule to the collection.
pivotGridControl1.FormatRules.Add(newRule)
End Sub
End Class
End Namespace
See Also