Home > front end >  How do you associate an object to a TGridColumns object
How do you associate an object to a TGridColumns object

Time:01-06

I am running Lazarus 0.9.30.

I have a standard TStringGrid on a form and have a function that dynamically adds TGridColumns objects to it at run time. I have a collection of objects that contain all the attributes of each column (that I read out of a file at run time), and I want to associate each object with its corresponding column header.

I have tried the code below but at run time when I try to access the object behind the column header object, I get a 'nil object returned. I suspect the reason this is occurring is that the grid cell (that holds the column title) is blank, and you can't associate objects with grid cells that are empty.

type
  TTmColumnTitles = class(TTmCollection)
  public
    constructor Create;
    destructor  Destroy; override;

    function  stGetHint(anIndex : integer) : string;
  end;

type
  TTmColumnTitle = class(TTmObject)
  private
    FCaption         : string;
    FCellWidth       : integer;
    FCellHeight      : integer;
    FFontOrientation : integer;
    FLayout          : TTextLayout;
    FAlignment       : TAlignment;
    FHint            : string;

    procedure vInitialise;

  public
    property stCaption        : string      read FCaption         write FCaption;
    property iCellWidth       : integer     read FCellWidth       write FCellWidth;
    property iCellHeight      : integer     read FCellHeight      write FCellHeight;
    property iFontOrientation : integer     read FFontOrientation write FFontOrientation;
    property Layout           : TTextLayout read FLayout          write FLayout;
    property Alignment        : TAlignment  read FAlignment       write FAlignment;
    property stHint           : string      read FHint            write FHint;

    constructor Create;
    destructor  Destroy; override;
  end;

procedure TTmMainForm.vLoadGridColumnTitles
  (
  aGrid       : TStringGrid;
  aCollection : TTmColumnTitles
  );
var
  GridColumn   : TGridColumn;
  aColumnTitle : TTmColumnTitle; //Just a pointer!
  anIndex1     : integer;
  anIndex2     : integer;
begin
  for anIndex1 := 0 to aCollection.Count - 1 do
    begin
      aColumnTitle := TTmColumnTitle(aCollection.Items[anIndex1]);

      GridColumn := aGrid.Columns.Add;
      GridColumn.Width := aColumnTitle.iCellWidth;
      GridColumn.Title.Font.Orientation := aColumnTitle.iFontOrientation;
      GridColumn.Title.Layout           := aColumnTitle.Layout;
      GridColumn.Title.Alignment        := aColumnTitle.Alignment;
      GridColumn.Title.Caption          := aColumnTitle.stCaption;

      aGrid.RowHeights[0] := aColumnTitle.iCellHeight;
      aGrid.Objects[anIndex1, 0] := aColumnTitle;
    end; {for}
end;

CodePudding user response:

Just assigning an object to the Objects property isn't enough. You have to draw the title caption from that object yourself in an OnDrawCell event handler, or assign the Cells property as well.

and you can't associate objects with grid cells that are empty

Yes you can. The string and the object of one cell 'work' independent of each other.

So it should be:

  for anIndex2 := 0 to aGrid.ColCount - 1 do 
  begin
    aColumnTitle := aCollection.Items[anIndex2];   // Is aCollection.Count in sync
                                                   // with aGrid.ColCount??
    aGrid.Cells[anIndex2, 0] := aColumnTitle.Caption;    
    aGrid.Objects[anIndex2, 0] := aColumnTitle;
  end;
  • Related