Features > Custom Cells > Custom Cells in code: CellFactory class |
FlexGrid comes with the CellFactory class to create every cell that appears on the grid. You can create custom cells by creating a class in your project that implements the ICellFactory interface and assign it to the CellFactory property of FlexGrid. Custom ICellFactory classes can be highly specialized and application-specific, or can be very generic and reusable. In general, custom ICellFactory classes are simpler than custom columns since they deal directly with cells. Implementing custom ICellFactory classes is fairly easy because you can inherit from the default CellFactory class included with the C1FlexGrid class. 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.
The following image shows custom cells created through CellFactory in FlexGrid.
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 WPF and Silverlight applications. Without virtualization, a grid would typically have to create several thousand visual elements at the same time, which would impact its performance.
The following code shows ICellFactory interface.
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 first method, CreateCell, creates FrameworkElement objects to represent cells. The parameters include the grid that owns the cells, the type of cell to create, and the CellRange to represent. 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 CreateCellEditor method is analogous to the first but creates a cell in edit mode. The last method, DisposeCell, is called after the cell is removed from the grid. It gives the caller a chance to dispose of any resources associated with the cell object.