2
votes

I am running Lazarus v0.9.30 (32 bit compiler).

This question is an extension of a previous question of mine.

The previous question revolved around how to change the orientation of text in the TGridColumns objects that I loaded at runtime into a standard TStringGrid. The solution involved overriding the DrawCellText event of the string grid.

My question is this. When I attempt to load the TStringGrid what I find is that the text orientation remains the same but the column cell height changes back to the default height.

The code I use to load the grid is shown below.

procedure TTmMainForm.vLoadWorldScoutGrid;
var
  aMember : TTmMember;
  anIndex1: integer;
  anIndex2: integer;
begin
  //Clear the string grid and set the row count to 1 to take into account the fixed row.
  SgWorldScout.Clear;
  SgWorldScout.RowCount := 1;

  for anIndex1 := 0 to Pred(FManager.Members.Count) do
  begin
    //Add a new row to the string grid.
    SgMembers.RowCount := SgMembers.RowCount + 1;

    //Get the TTmMember object from the collection.
    aMember := TTmMember(FManager.Members.Items[anIndex1]);

    //Populate the row cells in the string grid.
    SgMembers.Cells[0, SgMembers.RowCount - 1] := aMember.stMemberNumber;
    SgMembers.Cells[1, SgMembers.RowCount - 1] := aMember.stPatrol;
    SgMembers.Cells[2, SgMembers.RowCount - 1] := aMember.stSurname;
    SgMembers.Cells[3, SgMembers.RowCount - 1] := aMember.stFirstName;

    //Add the TTmMember object to every row cell.
    for anIndex2 := 0 to SgMembers.ColCount - 1 do
      SgMembers.Objects[anIndex2, SgMembers.RowCount - 1] := aMember;
  end; {for}}

  vSetWorldScoutGridPushbuttons;
end; 

I suspect that when I call 'SgWorldScout.Clear' that the properties of the string grid cells may get reset / modified as the default DrawCellText event gets called, which would explain the change in cell height. Not sure why the text orientation doesn't change either. Would someone be able to explain the behaviour of the DrawCellText event and why I am seeing this?

1
What exactly want you clear in this procedure ? Do you really want to clear the whole grid ? There is a set of functions, the TCustomStringGrid.Clean for cleaning only the cell content of the particular cell areas of the grid.TLama
Yes I want to clean the data rows only...not the fixed rows. So I would want to clear all of the non fixed cell text and any pointers to associated objects.user1174918
Then the answer is below :) You don't need to clear the whole grid. It's perfectly safe (and commonly used) to set the row count to the desired value, so by only setting the RowCount to 1 (without Clear) will keep your header (with all of its settings).TLama

1 Answers

2
votes

The Clear sets the RowCount and ColCount to 0 as you suspected. Then it's quite logical that the RowHeights is cleared too, because when you have RowCount set to 0 there are no heights to store. If you want to clear and add only the non fixed rows then simply set only the RowCount to 1 without clearing the whole grid. So modify your code this way:

procedure TTmMainForm.vLoadWorldScoutGrid;
var
  aMember : TTmMember;
  anIndex1: integer;
  anIndex2: integer;
begin
  // set the row count to 1 to keep the fixed row along with its settings
  SgWorldScout.RowCount := 1;

  for anIndex1 := 0 to Pred(FManager.Members.Count) do
  ...
end;