Solved

TStringGrid quick erase

Posted on 2000-04-19
5
268 Views
Last Modified: 2010-04-04
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)?
0
Comment
Question by:Softtech
  • 3
5 Comments
 
LVL 5

Expert Comment

by:TheNeil
ID: 2731256
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 =;)
0
 
LVL 4

Accepted Solution

by:
jeurk earned 50 total points
ID: 2731399
Hello,
the fastest way I know would be :

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  StringGrid1.RowCount := StringGrid1.FixedRows + 1;
  StringGrid1.Rows[StringGrid1.FixedRows].Clear;
end;

if you want to leave the rowcount intact just do :
procedure TForm1.BitBtn1Click(Sender: TObject);
var
  i: integer;
begin
  for i := StringGrid1.FixedRows to StringGrid1.RowCount - 1 do
  begin
    StringGrid1.Rows[i].Clear;
  end;
end;

Regards.
0
 
LVL 4

Expert Comment

by:jeurk
ID: 2805372
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
0
 

Author Comment

by:Softtech
ID: 2806349
I must have been in a bad mood when I graded you with a "B".  Sorry.
0
 
LVL 4

Expert Comment

by:jeurk
ID: 2809809
Ok, no problem then...
CU
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question