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 C1OlapGrid 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.
Visual Basic |
Copy Code
|
---|---|
Public Class ConditionalCellFactory Inherits C1.Silverlight.FlexGrid.CellFactory Public Overrides Function CreateCell(grid As C1FlexGrid, cellType__1 As CellType, range As CellRange) As FrameworkElement ' let base class to most of the work Dim cell = MyBase.CreateCell(grid, cellType__1, range) ' apply green background if necessary If cellType__1 = CellType.Cell Then Dim cellValue = grid(range.Row, range.Column) If TypeOf cellValue Is Double AndAlso CDbl(cellValue) > 100 Then Dim border = TryCast(cell, Border) border.Background = _greenBrush End If End If ' done Return cell End Function Shared _greenBrush As Brush = New SolidColorBrush(Color.FromArgb(&Hff, 88, 183, 112)) End Class |
C# |
Copy Code
|
---|---|
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:
Visual Basic |
Copy Code
|
---|---|
' apply conditional formatting to grid cells
_c1OlapPage.OlapGrid.CellFactory = New ConditionalCellFactory()
|
C# |
Copy Code
|
---|---|
// 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.