# Conditional Formatting Cells

Cells and rows can be styled based on data conditions using **ConditionalFormattingObjects**. The formatting objects could contain the following formatting properties for cell, row, and text alignment:

* **CellBackColor:** sets the background color of the cell.

* **CellForeColor:** sets the cell text color.

* **CellFont:** sets the font of the cell.

* **TextAlignment:** sets the alignment of the cell text.

* **RowBackColor:** sets the background color for the entire row that the cell appears in.

* **RowForeColor:** sets the cell text color for the entire row that the cell appears in.

* **RowFont:** sets the font of the row’s cells text.

* **RowTextAlignment:** sets the alignment of the row’s cells text.

The formatting properties of the rows apply if the **ApplyToRow** property of the formatting object is set to *true*. The **ApplyOnSelectedRows** property defines whether the formatting properties of the object will also apply to the selected rows.

The formatting objects are two different types, depending on the mechanism for defining conditions - rule-based and expression-based.

> note The **ConditionalFormattingObject** has lower priority than the modifications applied in the [ViewCellFormatting/CellFormatting](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/formatting-cells) event. The  ViewCellFormatting/CellFormatting will be applied last. It's not recommended to mix both approaches.

## Rule-based formatting objects

The rule-based formatting objects apply to the cells / rows according to the defined rules:

* **ConditionType:** sets conditions like *Equal*, *Contains*, etc.

* **TValue1:** a string representing the first value used to test the condition.

* **TValue2:** a string representing the second value used to test the condition.

The example below detects when a value in the second column `UnitPrice` has a value **greater than 30**. When the condition is met the cell background color is set to *SkyBlue*, the text color is set to *Red* and the text alignment is set to *MiddleRight*.

```C#
ConditionalFormattingObject obj = new ConditionalFormattingObject("MyCondition", ConditionTypes.Greater, "30", "", false);
obj.CellBackColor = Color.SkyBlue;
obj.CellForeColor = Color.Red;
obj.TextAlignment = ContentAlignment.MiddleRight;
this.radGridView1.Columns["UnitPrice"].ConditionalFormattingObjectList.Add(obj);
```

```VB.NET
Dim obj As New ConditionalFormattingObject("MyCondition", ConditionTypes.Greater, "30", "", False)
obj.CellBackColor = Color.SkyBlue
obj.CellForeColor = Color.Red
obj.TextAlignment = ContentAlignment.MiddleRight
Me.RadGridView1.Columns("UnitPrice").ConditionalFormattingObjectList.Add(obj)
```

> caption Figure 1: Rule-based formatting objects.

![WinForms RadGridView Rule based formatting objects](https://www.telerik.com/products/winforms/documentation/assets/a94549fa1328e7b6b0a04b5738be3dfe/gridview-cells-conditional-formatting-cells001.png)

## Expression-based formatting objects

The expression-based formatting objects apply to the cells/rows according to the defined **Expression**. It provides high flexibility to the formatting mechanism. The **Expression** could refer to data from more than one column. Its result should be Boolean. Function calls must be available in the active expression context; see [Functions Reference](https://www.telerik.com/products/winforms/documentation/controls/gridview/features/editing/expression-editor/functions-reference) for the built-in functions and custom registration behavior.

In order to validate an expression, it is suitable to use the [Expression editor](https://www.telerik.com/products/winforms/documentation/controls/gridview/features/editing/expression-editor/expression-editor).

> note The **ExpressionFormattingObject** has lower priority than the modifications applied in the [ViewCellFormatting/CellFormatting](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/formatting-cells) event. The ViewCellFormatting/CellFormatting will be applied last. It's not recommended to mix both approaches.

The example below detects when a value in the second column `UnitPrice` has a value **greater than 30** and sets styles to the `ProductName` column.

```C#
ExpressionFormattingObject obj = new ExpressionFormattingObject("MyCondition", "UnitPrice > 30", false);
obj.CellBackColor = Color.SkyBlue;
obj.CellForeColor = Color.Red;
this.radGridView1.Columns["ProductName"].ConditionalFormattingObjectList.Add(obj);
```

```VB.NET
Dim obj As New ExpressionFormattingObject("MyCondition", "UnitPrice > 30", False)
obj.CellBackColor = Color.SkyBlue
obj.CellForeColor = Color.Red
Me.RadGridView1.Columns("ProductName").ConditionalFormattingObjectList.Add(obj)
```

> caption Figure 2: Expression-based formatting objects.

![WinForms RadGridView Expression Based Formatting Objects](https://www.telerik.com/products/winforms/documentation/assets/b3f9636e2c1016b2b2804f089acde435/gridview-cells-conditional-formatting-cells002.png)

## Customize ConditionalFormattingForm

**RadGridView** provides a convenient form that the end user can use to create formatting objects:

> caption Figure 3: Conditional Formatting Form.

![WinForms RadGridView Conditional Formatting Form](https://www.telerik.com/products/winforms/documentation/assets/222d6d0c7921b4a72340e995de59532a/gridview-cells-conditional-formatting-cells003.png)

To access and customize the dialog, you can use the  **ConditionalFormattingFormShown** event. For example, here is how to disallow displaying of non-visible columns in the drop down and also, specify the format of displaying the columns as **name and header text**:

```C#
ExpressionFormattingObject obj = new ExpressionFormattingObject("MyCondition", "UnitPrice > 30", false);
obj.CellBackColor = Color.SkyBlue;
obj.CellForeColor = Color.Red;
this.radGridView1.Columns["ProductName"].ConditionalFormattingObjectList.Add(obj);
```

```VB.NET
Dim obj As New ExpressionFormattingObject("MyCondition", "UnitPrice > 30", False)
obj.CellBackColor = Color.SkyBlue
obj.CellForeColor = Color.Red
Me.RadGridView1.Columns("ProductName").ConditionalFormattingObjectList.Add(obj)
```

## See Also

* [Accessing and Setting the CurrentCell](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/accessing-and-setting-the-currentcell)
* [Accessing Cells](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/accessing-cells)
* [Creating Custom Cells](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/creating-custom-cells)
* [Formatting Cells](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/formatting-cells)
* [GridViewCellInfo](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/gridviewcellinfo)
* [Iterating Cells](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/iterating-cells)
* [Painting and Drawing in Cells](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/painting-and-drawing-in-cells)
* [ToolTips](https://www.telerik.com/products/winforms/documentation/controls/gridview/visual-elements/cells/tooltips)
