You are right. You have to keep track of where your columns are located. Maybe in a separate structure, or as a descendant object derived from TCustomGrid.
I keep a container object, where I store, among other things, the size of the columns, the type of the data they contain, the sort order, formatting options, and the position in the grid. And then I have a custom grid that references the container.
type
TpaGrid = class;
TpaColumnType = (ctText,ctDateTime,ctNumber,ctSize,ctPic,ctFileName);
TpaColumn = class(TCollectionItem)
private
FCaption: string;
FTitleFont: TFont;
FTitleAlignment: TAlignment;
FDataType : TPaColumnType;
FWidth: Integer;
FFont: TFont;
FColor: TColor;
FBackColor: TColor;
FAltBackColor: TColor;
FAlignment: TAlignment;
FPosition : integer;
FSortOrder : integer; // 0=no sort, 1=first, 2=second, etc...
FSortAscending : boolean;
// .... and many other interesting attributes
public
// ... published properties
end;
TpaColumnClass = class of TPaColumn;
TpaColumns = class(TCollection)
private
FGrid: TPaGrid;
// ... Getters and Setters, exposing the items as columns
public
constructor Create(grid:TPaGrid; ColumnClass: TPaColumnClass);
function AddColumn: TPaColumn;
// ... Load and Save procedures
// ... published properties
end;
TpaGrid = class (TStringGrid)
// ... overriden methods WMSize, DrawCell, ...
// ... getters and setters
private
FColumns : TpaColumns;
// ...
end;