ComponentOne FlexGrid for UWP
Custom Cells in Code
Features > Custom Cells > Custom Cells in Code
If you understand the ICellFactory class, you can click here to skip to the C1FlexGrid CellFactory implementation.

The grid 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 (columns, by contrast, need to deal with the columns themselves and also with the cells and other objects contained in the column).

The ICellFactory interface is very simple:

C#
Copy Code
public interface ICellFactory
 {
   FrameworkElement CreateCell(
     C1FlexGrid grid,
     CellType cellType,
     CellRange range);
   FrameworkElement CreateCellEditor(
     C1FlexGrid grid,
     CellType cellType,
     CellRange range)
   void DisposeCell(
     C1FlexGrid grid,
     CellType cellType,
     FrameworkElement cell);
 }

The ICellFactory interface calls three methods:

This method is responsible for creating FrameworkElement objects used to represent cells. The parameters include the grid that owns the cells, the type of cell to create, and the CellRange to be represented by the cells.

This method is nearly identical to the first, but it creates the cell in edit mode.

This method is called after the cell has been removed from the grid. It gives the caller a chance to dispose of any resources associated with the cell object.

When using custom cells, it is important to understand that grid cells are transient. Cells are constantly created and destroyed as the user scrolls, sorts, or selects ranges on the grid. This process is known as virtualization and is quite common in Windows Store applications. Without virtualization, a grid would typically have to create several thousand visual elements at the same time, which would ruin performance.

 

Implementation

Implementing custom ICellFactory classes is fairly easy because you can inherit from the default CellFactory class included with the C1FlexGrid.

The default CellFactory class was designed to be extensible, so you can let it handle all the details of cell creation and customize only what you need. In the following code, conditional formatting is applied to turn low values red and high values green:

C#
Copy Code
c1FlexGrid1.CellFactory = new ConditionalFormattingFactory();
 
class ConditionalFormattingFactory : CellFactory
{
    // create brushes used to indicate low and high values
    static Brush _brLowValue = new SolidColorBrush(Colors.Red);
    static Brush _brHighValue = new SolidColorBrush(Colors.Green);
 
    // overridden to apply the custom brushes based on the cell value
    public override void ApplyCellStyles(C1FlexGrid grid, CellType cellType, CellRange range, Border bdr)
    {
        // we are interested only in data cells (no headers)
        if (cellType == CellType.Cell)
        {
            // we are interested in double values only
            var col = grid.Columns[range.Column];
            if (col.DataType == typeof(double))
            {
                // get the cell value
                var value = (double)grid[range.Row, col];
 
                // apply formatting if value is out of range
                if (value < 100 || value > 1000)
                {
                    var tb = bdr.Child as TextBlock;
                    if (tb != null)
                    {
                        bdr.Background = value < 100 ? _brLowValue : _brHighValue;
                    }
                }
            }
        }
    }
}