Customizing Appearance / Customizing Cells / Applying Conditional Formatting
Applying Conditional Formatting

C1FlexSheet allows you to change the appearance of an individual cell or a range of cells using conditional formatting. Conditional formatting can be used to compare different data and highlight important data.
Conditional Formatting

In the above image, you see that data is formatted with different font color and background color depending on some conditions. The numbers divisible by 3 appear red, numbers divisible by 4 appear blue, and the numbers which are neither divisible by 3 nor 4 appear with beige background color.

The following code illustrates the conditions that we have set to change the appearance of data in cells:

Public Class ConditionalCellFactory
    Inherits CellFactory
    Public Overrides Sub ApplyCellStyles(grid As C1FlexGrid, cellType As CellType,
                                         rng As CellRange, bdr As Border)
        MyBase.ApplyCellStyles(grid, cellType, rng, bdr)

        If cellType = cellType.Cell Then
            Dim tb = If(TypeOf bdr.Child Is TextBlock, TryCast(bdr.Child, TextBlock),
            TryCast(DirectCast(bdr.Child, System.Windows.Controls.Grid).Children(1), TextBlock))

            If grid(rng.Row, rng.Column) IsNot Nothing Then
                If CDbl(grid(rng.Row, rng.Column)) Mod 3 = 0 Then
                    tb.Foreground = New SolidColorBrush(Colors.Red)
                ElseIf CDbl(grid(rng.Row, rng.Column)) Mod 4 = 0 Then
                    tb.Foreground = New SolidColorBrush(Colors.Blue)
                Else
                    bdr.Background = New SolidColorBrush(Colors.Beige)
                End If
            End If
        End If
    End Sub
End Class

In the above code, a class named ConditionalCellFactory is used which inherits from CellFactory class of C1FlexGrid control. The ConditionalCellFactory class contains all the implementation of conditional formatting that we have set.