Custom Cells in code: CellFactory class

The C1FlexGrid control uses a very different approach. 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:

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 first method, CreateCell, 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. The CellType parameter specifies whether the cell being created is a regular data cell, a row or column header, or the fixed cells at the top left and bottom right of the grid.

The second method, CreateCellEditor, is analogous to the first but creates a cell in edit mode.

The last method, DisposeCell, is called after the cell has been removed from the grid. If 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 Silverlight and WPF applications. Without virtualization, a grid would typically have to create several thousand visual elements at the same time, which would ruin performance.

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 class was designed to be extensible, so you can let it handle all the details of cell creation and customize only what you need.

The following sections describe examples of grids that use custom ICellFactory classes used to implement functionality we hope you will find interesting. The examples are part of the MainTestApplication included with this document, which includes both Silverlight and WPF versions of every sample. The descriptions in this document focus on key implementation points. For details, please refer to the sample application source code.

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