# Conditional Formatting | WPF Controls | DevExpress Documentation

The [Pivot Grid](/WPF/7228/controls-and-libraries/pivot-grid) control includes a Microsoft Excel-inspired conditional formatting feature, which 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.

[Run Demo](dxdemo://Wpf/DXPivotGrid/MainDemo/ConditionalFormatting) [Watch Video](https://www.youtube.com/watch?v=oA3sARCxOvk)

## Conditional Formatting Overview

Conditional formatting allows you to change the appearance of data cells based on specific conditions. Use the [FormatConditionBase.MeasureName](/WPF/DevExpress.Xpf.PivotGrid.FormatConditionBase.MeasureName) property to specify a data field whose values should be formatted. Then, use the field’s [PivotGridField.Name](/WPF/DevExpress.Xpf.PivotGrid.PivotGridField.Name) property value to refer to the field.

Important

It is necessary to set the [PivotGridField.Name](/WPF/DevExpress.Xpf.PivotGrid.PivotGridField.Name) property for all fields in order for the conditional formatting to work properly.

To apply a format condition, create a new instance of the required format type (the [FormatConditionBase](/WPF/DevExpress.Xpf.PivotGrid.FormatConditionBase) class descendant), specify its parameters and add it to the [PivotGridControl.FormatConditions](/WPF/DevExpress.Xpf.PivotGrid.PivotGridControl.FormatConditions) collection. The following formatting types are available by default.

| Condition Type | Condition Class | Description |
| --- | --- | --- |
| Format using color scales | [ColorScaleFormatCondition](/WPF/DevExpress.Xpf.PivotGrid.ColorScaleFormatCondition) | Allows you to display data distribution and variation using a gradation of colors. |
| Format using data bars | [DataBarFormatCondition](/WPF/DevExpress.Xpf.PivotGrid.DataBarFormatCondition) | Applies a format using a data bar. The bar length changes proportionally to a cell value. A longer bar corresponds to a higher value, and a shorter bar corresponds to a lower value. |
| Format using icons | [IconSetFormatCondition](/WPF/DevExpress.Xpf.PivotGrid.IconSetFormatCondition) | An icon set format allows you to classify column values into several ranges separated by threshold values, and display a specific icon in a column cell according to the range to which this cell value belongs. |
| Format only top or bottom rank values, or values that are above or below average | [TopBottomRuleFormatCondition](/WPF/DevExpress.Xpf.PivotGrid.TopBottomRuleFormatCondition) | Applies a format if a value is in the range of the highest/lowest fields intersection data values or if a cell value(s) is above or below the fields intersection data average. |
| Format based on value(s) or user-defined expression(s) | [FormatCondition](/WPF/DevExpress.Xpf.PivotGrid.FormatCondition) | Applies a format if a column’s value meets a specified condition (Equal, Less, Between, etc.) or expression. |

You can apply the rule to all data cells, or a specified row and column intersection.

- To apply format conditions to a specified row and column intersection, set the condition’s [FormatConditionBase.ApplyToSpecificLevel](/WPF/DevExpress.Xpf.PivotGrid.FormatConditionBase.ApplyToSpecificLevel) property to **true** and specify both the column and row using the [FormatConditionBase.ColumnName](/WPF/DevExpress.Xpf.PivotGrid.FormatConditionBase.ColumnName) and [FormatConditionBase.RowName](/WPF/DevExpress.Xpf.PivotGrid.FormatConditionBase.RowName) properties, respectively.
- To apply format condition to all data cells, make sure that the condition’s [FormatConditionBase.ApplyToSpecificLevel](/WPF/DevExpress.Xpf.PivotGrid.FormatConditionBase.ApplyToSpecificLevel) is set to **false**. In this case, the [FormatConditionBase.ColumnName](/WPF/DevExpress.Xpf.PivotGrid.FormatConditionBase.ColumnName) and [FormatConditionBase.RowName](/WPF/DevExpress.Xpf.PivotGrid.FormatConditionBase.RowName) properties are ignored.

Note

Note that the [TopBottomRuleFormatCondition](/WPF/DevExpress.Xpf.PivotGrid.TopBottomRuleFormatCondition) rule is not an effect for all data cells. Specify the row and column intersection to make this rule work correctly.

Note

When printing the Pivot Grid control and exporting it to PDF, HTML, MHT, RTF and XLS(X) formats, conditional formatting using icons and data bars is not printed/exported.

## Create Format Conditions at Design Time

You can edit format conditions at design time in three ways: using the design-time Conditional Formatting Rules Manager using the Format Condition Collection Editor, or manually creating format condition in XAML.

**Design-time Conditional Formatting Rules Manager**

![Conditional Formatting - Invoking Design-time Manager](/WPF/images/conditional-formatting-invoking-design-time-manager118341.png)

In the design view, right-click the Pivot Grid’s smart tag menu and select **Manage Conditional Formatting Rules** to invoke the conditional formatting manager.

**Format Condition Collection Editor**

![Conditional Formatting - Format Condition Editor](/WPF/images/conditional-formatting-format-condition-editor118340.png)

Click the ellipsis button for the [PivotGridControl.FormatConditions](/WPF/DevExpress.Xpf.PivotGrid.PivotGridControl.FormatConditions) to invoke the Collection Editor.

**Create format conditions in XAML**
A new instance of the PivotGridControl is created with an empty collection of style format conditions. Use the [PivotGridControl.FormatConditions](/WPF/DevExpress.Xpf.PivotGrid.PivotGridControl.FormatConditions) property to access the collection of [FormatConditionCollection](/WPF/DevExpress.Xpf.PivotGrid.FormatConditionCollection) objects containing style format conditions. You can manage this collection of rules both at design time and runtime.

Below you can see the data bar format condition, applied to the intersection of the ‘Sales Person’ and ‘Quarter’ fields. The ‘Variation’ field is set as measure. Positive values fill in with green-white gradient color, and negative values fill in with red-white gradient color.

```
<dxpg:PivotGridControl.FormatConditions>
    <dxpg:DataBarFormatCondition ApplyToSpecificLevel="True" ColumnName="fieldQuarter" 
                   MeasureName="fieldVariation" RowName="fieldSalesPerson">
        <dx:DataBarFormat BorderBrush="#FF63C384" BorderBrushNegative="#FFFF555A">
            <dx:DataBarFormat.FillNegative>
                <LinearGradientBrush EndPoint="1,0">
                    <GradientStop Color="White" Offset="0"/>
                    <GradientStop Color="#FFFF555A" Offset="1"/>
                </LinearGradientBrush>
            </dx:DataBarFormat.FillNegative>
            <dx:DataBarFormat.Fill>
                <LinearGradientBrush EndPoint="1,0">
                    <GradientStop Color="#FF63C384" Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </dx:DataBarFormat.Fill>
        </dx:DataBarFormat>
    </dxpg:DataBarFormatCondition>
</dxpg:PivotGridControl.FormatConditions>
```

## Create Format Conditions at Runtime

To create a format condition at runtime, right-click the data cell, to which fields intersection you want to add a format condition, and select **Conditional Formatting** to invoke the conditional formatting menu. This menu is available when the [PivotGridControl.AllowConditionalFormattingMenu](/WPF/DevExpress.Xpf.PivotGrid.PivotGridControl.AllowConditionalFormattingMenu) property is set to **true**.

![Conditional Formatting - Context menu](/WPF/images/conditional-formatting-context-menu118218.png)

Select the required condition, specify its parameters, measure name and intersection of row and column fields, and then click OK. Besides, you can manage format conditions at runtime using the **Conditional Formatting Rules Manager**.

## Conditional Formatting Rules Manager

You can create, sort and modify the created rules at runtime and design time using the **Conditional Formatting Rules Manager**. This Manager is available both at design time and runtime. To invoke it at runtime, select the **Manage Rules** item from the Conditional Formatting context menu. In the design view, right-click the Pivot Grid’s smart tag menu and select **Manage Conditional Formatting Rules** to invoke the conditional formatting manager.

![Conditional Formatting - Manager](/WPF/images/conditional-formatting-manager118217.png)

- To **create** a new rule, click the **New Rule…** button, select a rule type and specify its parameters. Depending on rule type, you can set maximum and minimum values, specify format, colors or icon style, etc.
- To **edit** the existent rule, click the **Edit Rule…** button and change the rule parameters. Besides that, you can see the rule’s format preview and specify the row, measure and column in the list of rules in the main window.
  Note that you can apply a rule to all data cells, or a specified row and column intersection. Select fields in the **Row** and **Column** drop-down lists to apply a format condition to the specified intersection. To apply a format condition to all data cells instead of intersection, set the **Column** and **Row** values to *(Any)*.
- To **delete** the rule, select the rule and click the **Delete Rule** button.
- To **reorder** rules, select the rule and click **Up** or **Down** button.

Note

Set the control’s [PivotGridControl.AllowConditionalFormattingManager](/WPF/DevExpress.Xpf.PivotGrid.PivotGridControl.AllowConditionalFormattingManager) property to **true** to make the Conditional Formatting Manager available for end users.

## Example

This example shows how to add format conditions to WPF Pivot Grid Control.

- The **Data Bar** conditional formatting is applied to the ‘Extended Price’ measure and intersection of the ‘Sales Person’ and ‘Quarter’ fields. This condition formats data cells with a predefined orange gradient data bar.
- The **Top Bottom Rule** conditional formatting is applied to the ‘Quantity’ measure and intersection of the ‘Sales Person’ and ‘Quarter’ fields. This condition formats data cells whose values are above average with green text and light green fill.
- The **Icon Set** conditional formatting is applied to the ‘Extended Price’ measure and intersection of the ‘Sales Person’ and ‘Year’ fields. This condition displays a specific icon in a cell according to the range to which this cell value belongs.

The image below shows the result.

![Pivot Grid Conditional Formatting example](/WPF/images/pivot-grid-conditional-formatting-example118543.png)

[View Example](https://github.com/DevExpress-Examples/wpf-pivot-grid-apply-format-conditions-to-data-cells)

- MainWindow.xaml.cs
- MainWindow.xaml
- MainWindow.xaml.vb

<section id="tabpanel_z8iigf8iEE_tabid-csharpMainWindow-xaml-cs" role="tabpanel" data-tab="tabid-csharpMainWindow-xaml-cs">
<pre><code data-code-links="{&quot;/ (System.Windows)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system.windows&quot;,&quot;/ (System)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system&quot;,&quot;/ (DevExpress.Xpf.PivotGrid)(?:;|$)/&quot;:&quot;/WPF/DevExpress.Xpf.PivotGrid&quot;}" class="lang-csharp">using System.Windows;
using System;
using DevExpress.Xpf.PivotGrid;

namespace WpfPivotGridConditionalFormatting
{
    /// &lt;summary&gt;
    /// Interaction logic for MainWindow.xaml
    /// &lt;/summary&gt;
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            FilterFieldValues(fieldYear, new int[]{2016}, FieldFilterType.Included);

            // Creates a new DataBarFormatCondition instance.
            DataBarFormatCondition formatRulesDataBar = new DataBarFormatCondition();

            // Adds this instance to the FormatConditionCollection.
            pivotGridControl1.AddFormatCondition(formatRulesDataBar);

            // Specifies a column field.
            formatRulesDataBar.ColumnName = &quot;fieldQuarter&quot;;

            // Specifies a row field.
            formatRulesDataBar.RowName = &quot;fieldSalesPerson&quot;;

            // Specifies a data field.
            formatRulesDataBar.MeasureName = &quot;fieldExtendedPrice&quot;;

            // Applies the condition to intersection of row and column fields.
            formatRulesDataBar.ApplyToSpecificLevel = true;

            // Sets the predefined format.
            formatRulesDataBar.PredefinedFormatName = &quot;OrangeGradientDataBar&quot;;

        }

        private void FilterFieldValues(PivotGridField field, int[] filterValues, 
            FieldFilterType filterType)
        {
            pivotGridControl1.BeginUpdate();
            try
            {
                field.FilterValues.Clear(); 
                foreach (object filterValue in filterValues)
                    field.FilterValues.Add(filterValue);
            }
            finally
            {
                field.FilterValues.FilterType = filterType;
                pivotGridControl1.EndUpdate();
            }
        }
    }
}
</code></pre></section>
<section id="tabpanel_z8iigf8iEE_tabid-xamlMainWindow-xaml" role="tabpanel" data-tab="tabid-xamlMainWindow-xaml" aria-hidden="true" hidden="hidden">
<pre><code class="lang-xaml">&lt;Window x:Class=&quot;WpfPivotGridConditionalFormatting.MainWindow&quot;
        xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
        xmlns:dxpg=&quot;http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid&quot;
        xmlns:dx=&quot;http://schemas.devexpress.com/winfx/2008/xaml/core&quot;
        xmlns:my=&quot;clr-namespace:WpfPivotGridConditionalFormatting.nwindDataSetTableAdapters&quot; 
        xmlns:my1=&quot;clr-namespace:WpfPivotGridConditionalFormatting&quot;
        dx:ThemeManager.Theme=&quot;Office2013LightGray&quot; 
        Title=&quot;Pivot Grid Conditional Formatting&quot; 
        Height=&quot;427&quot; Width=&quot;755&quot; &gt;
    &lt;Window.Resources&gt;
        &lt;dx:TypedSimpleSource x:Key=&quot;TypedSimpleSource&quot; 
                              AdapterType=&quot;my:SalesPersonTableAdapter&quot; 
                              ContextType=&quot;my1:nwindDataSet&quot; 
                              Path=&quot;SalesPerson&quot;&gt;
            &lt;dx:DesignDataManager.DesignData&gt;
                &lt;dx:DesignDataSettings RowCount=&quot;5&quot; /&gt;
            &lt;/dx:DesignDataManager.DesignData&gt;
        &lt;/dx:TypedSimpleSource&gt;
    &lt;/Window.Resources&gt;
    &lt;Grid&gt;
        &lt;dxpg:PivotGridControl Name=&quot;pivotGridControl1&quot; 
                               DataSource=&quot;{Binding Path=Data, Source={StaticResource TypedSimpleSource}}&quot; 
                               AllowConditionalFormattingMenu=&quot;True&quot;&gt;
            &lt;dxpg:PivotGridControl.FormatConditions&gt;
                &lt;dxpg:IconSetFormatCondition ApplyToSpecificLevel=&quot;True&quot; 
                                             MeasureName=&quot;fieldExtendedPrice&quot; 
                                             RowName=&quot;fieldSalesPerson&quot; ColumnName=&quot;fieldYear&quot;&gt;
                    &lt;dxpg:IconSetFormatCondition.Format&gt;
                    &lt;dx:IconSetFormat&gt;
                        &lt;dx:IconSetElement Threshold=&quot;66.666666666666671&quot; ThresholdComparisonType=&quot;GreaterOrEqual&quot;&gt;
                            &lt;dx:IconSetElement.Icon&gt;
                                &lt;BitmapImage UriCachePolicy=&quot;{x:Null}&quot; 
        UriSource=&quot;pack://application:,,,/DevExpress.Xpf.Core.v15.1;component/Core/ConditionalFormatting/Images/IconSets/Arrows3_1.png&quot; /&gt;
                            &lt;/dx:IconSetElement.Icon&gt;
                        &lt;/dx:IconSetElement&gt;
                        &lt;dx:IconSetElement Threshold=&quot;33.333333333333336&quot; ThresholdComparisonType=&quot;GreaterOrEqual&quot;&gt;
                            &lt;dx:IconSetElement.Icon&gt;
                                &lt;BitmapImage UriCachePolicy=&quot;{x:Null}&quot; 
         UriSource=&quot;pack://application:,,,/DevExpress.Xpf.Core.v15.1;component/Core/ConditionalFormatting/Images/IconSets/Arrows3_2.png&quot; /&gt;
                            &lt;/dx:IconSetElement.Icon&gt;
                        &lt;/dx:IconSetElement&gt;
                        &lt;dx:IconSetElement Threshold=&quot;0&quot; ThresholdComparisonType=&quot;GreaterOrEqual&quot;&gt;
                            &lt;dx:IconSetElement.Icon&gt;
                                &lt;BitmapImage UriCachePolicy=&quot;{x:Null}&quot; 
         UriSource=&quot;pack://application:,,,/DevExpress.Xpf.Core.v15.1;component/Core/ConditionalFormatting/Images/IconSets/Arrows3_3.png&quot; /&gt;
                            &lt;/dx:IconSetElement.Icon&gt;
                        &lt;/dx:IconSetElement&gt;
                        &lt;/dx:IconSetFormat&gt;
                    &lt;/dxpg:IconSetFormatCondition.Format&gt;
                &lt;/dxpg:IconSetFormatCondition&gt;
                &lt;dxpg:TopBottomRuleFormatCondition ApplyToSpecificLevel=&quot;True&quot; 
                                                   ColumnName=&quot;fieldQuarter&quot; 
                                                   MeasureName=&quot;fieldQuantity&quot; 
                                                   RowName=&quot;fieldSalesPerson&quot; 
                                                   Rule=&quot;TopItems&quot;&gt;
                    &lt;dxpg:TopBottomRuleFormatCondition.Format&gt;
                        &lt;dx:Format Background=&quot;LightGreen&quot; Foreground=&quot;Green&quot; /&gt;
                    &lt;/dxpg:TopBottomRuleFormatCondition.Format&gt;
                &lt;/dxpg:TopBottomRuleFormatCondition&gt;
            &lt;/dxpg:PivotGridControl.FormatConditions&gt;
            &lt;dxpg:PivotGridControl.Fields&gt;
                &lt;dxpg:PivotGridField Area=&quot;RowArea&quot; FieldName=&quot;Country&quot; 
                                     Name=&quot;fieldCountry&quot; AreaIndex=&quot;0&quot; /&gt;
                &lt;dxpg:PivotGridField Area=&quot;DataArea&quot; FieldName=&quot;Extended Price&quot; 
                                     Name=&quot;fieldExtendedPrice&quot; AreaIndex=&quot;0&quot; /&gt;
                &lt;dxpg:PivotGridField Area=&quot;ColumnArea&quot; Caption=&quot;Year&quot; FieldName=&quot;OrderDate&quot;
                                     Name=&quot;fieldYear&quot; GroupInterval=&quot;DateYear&quot; AreaIndex=&quot;0&quot; /&gt;
                &lt;dxpg:PivotGridField Area=&quot;ColumnArea&quot; Caption=&quot;Quarter&quot; FieldName=&quot;OrderDate&quot; 
                                     Name=&quot;fieldQuarter&quot; GroupInterval=&quot;DateQuarter&quot; AreaIndex=&quot;1&quot; ValueFormat=&quot;Quarter {0}&quot; /&gt;
                &lt;dxpg:PivotGridField Area=&quot;DataArea&quot; FieldName=&quot;Quantity&quot; 
                                     Name=&quot;fieldQuantity&quot; AreaIndex=&quot;1&quot; /&gt;
                &lt;dxpg:PivotGridField Area=&quot;RowArea&quot; FieldName=&quot;Sales Person&quot; 
                                     Name=&quot;fieldSalesPerson&quot; AreaIndex=&quot;1&quot; /&gt;
            &lt;/dxpg:PivotGridControl.Fields&gt;
        &lt;/dxpg:PivotGridControl&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;
</code></pre></section>
<section id="tabpanel_z8iigf8iEE_tabid-vbMainWindow-xaml-vb" role="tabpanel" data-tab="tabid-vbMainWindow-xaml-vb" aria-hidden="true" hidden="hidden">
<pre><code data-code-links="{&quot;/ (System.Windows)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system.windows&quot;,&quot;/ (System)(?:;|$)/&quot;:&quot;https://learn.microsoft.com/dotnet/api/system&quot;,&quot;/ (DevExpress.Xpf.PivotGrid)(?:;|$)/&quot;:&quot;/WPF/DevExpress.Xpf.PivotGrid&quot;}" class="lang-vb">Imports System.Windows
Imports System
Imports DevExpress.Xpf.PivotGrid

Namespace WpfPivotGridConditionalFormatting
    &#39;&#39;&#39; &lt;summary&gt;
    &#39;&#39;&#39; Interaction logic for MainWindow.xaml
    &#39;&#39;&#39; &lt;/summary&gt;
    Partial Public Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()

            FilterFieldValues(fieldYear, New Integer(){2016}, FieldFilterType.Included)

            &#39; Creates a new DataBarFormatCondition instance.
            Dim formatRulesDataBar As New DataBarFormatCondition()

            &#39; Adds this instance to the FormatConditionCollection.
            pivotGridControl1.AddFormatCondition(formatRulesDataBar)

            &#39; Specifies a column field.
            formatRulesDataBar.ColumnName = &quot;fieldQuarter&quot;

            &#39; Specifies a row field.
            formatRulesDataBar.RowName = &quot;fieldSalesPerson&quot;

            &#39; Specifies a data field.
            formatRulesDataBar.MeasureName = &quot;fieldExtendedPrice&quot;

            &#39; Applies the condition to intersection of row and column fields.
            formatRulesDataBar.ApplyToSpecificLevel = True

            &#39; Sets the predefined format.
            formatRulesDataBar.PredefinedFormatName = &quot;OrangeGradientDataBar&quot;

        End Sub

        Private Sub FilterFieldValues(ByVal field As PivotGridField,
                                      ByVal filterValues() As Integer,
                                      ByVal filterType As FieldFilterType)
            pivotGridControl1.BeginUpdate()
            Try
                field.FilterValues.Clear()
                For Each filterValue As Object In filterValues
                    field.FilterValues.Add(filterValue)
                Next filterValue
            Finally
                field.FilterValues.FilterType = filterType
                pivotGridControl1.EndUpdate()
            End Try
        End Sub
    End Class
End Namespace
</code></pre></section>