Conditional Formatting

The C1OlapGrid control derives from the C1FlexGrid control, so you can use the grid’s custom cells features to apply styles to cells based on their contents. This sample shows a grid where values greater than 100 appear with a light green background.

The C1FlexGrid control has a CellFactory class that is responsible for creating every cell shown on the grid. To create custom cells, you have to create a class that implements the ICellFactory interface and assign this class to the grid's CellFactory property. Like custom columns, custom ICellFactory classes can be highly specialized and application-specific, or they can be general, reusable, configurable classes. In general, custom ICellFactory classes are a lot simpler than custom columns since they deal directly with cells.

Here is the code which implements a ConditionalCellFactory class responsible applying a custom green background to cells with values over 100.

public class ConditionalCellFactory : C1.Silverlight.FlexGrid.CellFactory

{

    public override FrameworkElement CreateCell(C1FlexGrid grid, CellType cellType, CellRange range)

    {

        // let base class to most of the work

        var cell = base.CreateCell(grid, cellType, range);

 

        // apply green background if necessary

        if (cellType == CellType.Cell)

        {

            var cellValue = grid[range.Row, range.Column];

            if (cellValue is double && (double)cellValue > 100)

            {

                var border = cell as Border;

                border.Background = _greenBrush;

            }

        }

 

        // done

        return cell;

    }

    static Brush _greenBrush = new SolidColorBrush(Color.FromArgb(0xff, 88, 183, 112));

}

And here is the code required to use this on our C1OlapGrid:

// apply conditional formatting to grid cells

_c1OlapPage.OlapGrid.CellFactory = new ConditionalCellFactory();

If you were to add this code to a previous example, you would see how this appears at run-time.

 


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.