wpf-118908-controls-and-libraries-data-grid-conditional-formatting-conditional-formats-formatting-above-or-below-average-values.md
The Average conditional format allows you to format cells whose values are above or below an average value.
The image below shows the grid column whose Profit cells are red if their values are above average.
This topic consists of the following sections:
Create the TopBottomRuleFormatCondition class instance and specify the following settings to create a conditional format in code:
Specify the rule type (TopBottomRule.AboveAverage or TopBottomRule.BelowAverage) using the TopBottomRuleFormatCondition.Rule property.
Use the FormatConditionBase.FieldName property to specify the column’s field name to which the conditional format should apply.
Specify the target cells’ formatting:
Add the resulting TopBottomRuleFormatCondition instance to the TableView.FormatConditions (or TreeListView.FormatConditions) collection.
The following code sample illustrates how to define a conditional format in markup:
<dxg:TableView.FormatConditions>
<dxg:TopBottomRuleFormatCondition Rule="AboveAverage" FieldName="Profit" PredefinedFormatName="LightRedFillWithDarkRedText" />
</dxg:TableView.FormatConditions>
The code sample below illustrates how to define the same conditional format in code-behind:
var profitFormatCondition = new TopBottomRuleFormatCondition() {
Rule = TopBottomRule.AboveAverage,
FieldName = "Profit",
PredefinedFormatName = "LightRedFillWithDarkRedText"
};
view.FormatConditions.Add(profitFormatCondition);
Dim profitFormatCondition = New TopBottomRuleFormatCondition() With {
.Rule = TopBottomRule.AboveAverage,
.FieldName = "Profit",
.PredefinedFormatName = "LightRedFillWithDarkRedText"
}
view.FormatConditions.Add(profitFormatCondition)
See Also