Link to home
Start Free TrialLog in
Avatar of Softtech
Softtech

asked on

TStringGrid quick erase

I was looking to quickly erase a populated TStringGrid, and had thought I'd find a .Erase method, but it appears such a method does not exist.  Is there a quick was to erase the contents of the cells in a TStringGrid without havig to use a For/Next loop (which would have to iterate through all the cells and erase them)?
Avatar of TheNeil
TheNeil

You could just iterate through all of the rows
e.g.
FOR iCount := 0 TO (Grid.ColCount - 1)
DO
  Grid.Cells[iCount, 1] := '';

FOR iCount := 1 TO (Grid.RowCount - 1)
DO
  Grid.Rows[iCount] := Grid.Rows[1];

This basically wipes the first row (ignoring the top row which might contain titles) and then copies that row into all the rest. Not ideal but a bit quicker

The Neil =;)
ASKER CERTIFIED SOLUTION
Avatar of jeurk
jeurk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello,
May I ask you why you only give me a B ?
Did you not liked the answer ?

the best answer would be :
procedure TForm1.BitBtn1Click(Sender: TObject);
var
   OldRowCount:integer;
begin
  //save the rowcount for restoring later
  OldRowCount:=StringGrid1.RowCount;
  //clear the whole lines at once
  StringGrid1.RowCount := StringGrid1.FixedRows + 1;
  StringGrid1.Rows[StringGrid1.FixedRows].Clear;
  StringGrid1.RowCount:=OldRowCount+StringGrid1.FixedRows ;
end;

I'm asking you so I can enhance what I say next time...
CU
Avatar of Softtech

ASKER

I must have been in a bad mood when I graded you with a "B".  Sorry.
Ok, no problem then...
CU