Example 51 : cell property access, clipboard persistent cell properties and binary saveTwo approaches exist in TAdvStringGrid to set cell drawing properties. Setting cell color, font colors and style, alignments can be done with the events OnGetCellColor and OnGetAlignment. This approach provides an effective method for setting the cell characteristics as there is no memory overhead required per cell to store the cell properties. An additional advantage of using the OnGetCellColor and OnGetAlignment events is that the properties can be set dynamically depending on the content of the cell. A typical example is setting font colors black for positive numbers and red for negative numbers. Writing a single OnGetCellColor event takes care of this and no additional memory is required to store the color values.
In some circumstances, cell property handling becomes very complex when using the event based method. In this case, the cell drawing properties can be set with a set of properties:
Grid.Colors[col,row]: TColor : sets the background color of the cell, when color is clNone, default color is used.
Grid.FontColors[col,row]: TColor : sets the font color of the cell, when color is clNone, default color is used.
Grid.FontSizes[col,row]: Integer : sets the font size of the cell, when font size is 0, default font size is used.
Grid.FontStyles[col,row]: TFontStyle : sets the font style of the cell, when font style is , default font style is used.
Grid.FontNames[col,row]: string : sets the font name of the cell, when the string is empty, the default font is used.
Grid.Alignments[col,row]: TAlignment : sets the horizontal alignment in the cell.
Several things should be considered when using these cell properties:
- New methods SaveToBinFile, LoadFromBinFile, SaveToBinStream, LoadFromBinStream make grid contents including cell properties persistent in file or stream
- Setting the property Grid.Navigation.AllowFmtClipboard allow to copy and paste both grid cell content and properties to the clipboard. Full content and property copy & paste works in a single application as well as inter applications with TAdvStringGrid. This clipboard format also supports bitmaps, imagelist images, icons, pictures, checkboxes, radiobuttons, comments that are created in a cell
- The events OnGetCellColor and OnGetAlignment can still be used and override the properties set for a cell
- It is not necessary to set all properties for a cell. Where no properties are set, the default grid settings are used
- For merged cells, the properties for all cells that are merged are taken from the top left cell
- Cell merging state is also considered as a property of a cell and is thus also persistent with the SaveToBinFile, LoadFromBinFile methods
if not BoldButton.Down then begin with TAdvStringGrid1 do FontStyles[col,row] := FontStyles[col,row] - [fsBold]; end else begin with TAdvStringGrid1 do FontStyles[col,row] := FontStyles[col,row] + [fsBold]; end;
The grid contents, including all the cell properties are now saved or loaded from a file with:
Grid.LoadFromBinFile(filename) and Grid.SaveToBinFile(filename)
Delphi project & source files for downloading included in the main demos distribution for Delphi.