Board index » delphi » Clearing all cells in a StringGrid

Clearing all cells in a StringGrid

I'm using a StringGrid, existing of 40 columns and 1000 rows.
It works fine, except when I'm clearing all cells. It takes to
long time.
The only way I have found out to clear all cells are by clearing cell by
cell.

I do this in a loop as following example:

 for Row := 0 to 999
          for Col := 0 to 39 do
            StringGrid.Cells[ACol, ARow] := '';

Does anyone know a faster way to clear all cells in a StringGrid ?

Regards
Stefan

 

Re:Clearing all cells in a StringGrid


Stefan Svensson schrieb in Nachricht
<7lkoq8$4...@forums.borland.com>...

Quote
>I'm using a StringGrid, existing of 40 columns and 1000 rows.
>It works fine, except when I'm clearing all cells. It takes to
>long time.
>The only way I have found out to clear all cells are by clearing cell
by
>cell.

>I do this in a loop as following example:

> for Row := 0 to 999
>          for Col := 0 to 39 do
>            StringGrid.Cells[ACol, ARow] := '';

>Does anyone know a faster way to clear all cells in a StringGrid ?

>Regards
>Stefan

I think, this problem occurs because changing the value of a
Cell-property forces Updating the StringGrid, and this costs time.
Perhaps it is faster to use the Rows-property or Cols-property of the
Stringgrid (TStrings class. Please do not forget to try the
BeginUpdate and the EndUpDate method of TStrings, and Invalidating the
Grid after Clearing).

I hope, I could help you a little bit

Hans-Jrgen

- Show quoted text -

Quote

Re:Clearing all cells in a StringGrid


Hi Stefan,

You can use the Clear() method of TStrings...

for Col := 0 to 39 do
    StringGrid.Cols[Col].Clear();

HTH.

--------------------------------------
Damon Chandler

http://bcbcaq.freeservers.com
Answers to <Commonly Asked Questions>

Re:Clearing all cells in a StringGrid


Thank's for your help and quick replay.
I will try both of your suggestions right away.

Regards // stefan

Quote
Stefan Svensson <stefan.svens...@mbox2.swipnet.se> wrote in message

news:7lkoq8$4rk8@forums.borland.com...
Quote
> I'm using a StringGrid, existing of 40 columns and 1000 rows.
> It works fine, except when I'm clearing all cells. It takes to
> long time.
> The only way I have found out to clear all cells are by clearing cell by
> cell.

> I do this in a loop as following example:

>  for Row := 0 to 999
>           for Col := 0 to 39 do
>             StringGrid.Cells[ACol, ARow] := '';

> Does anyone know a faster way to clear all cells in a StringGrid ?

> Regards
> Stefan

Re:Clearing all cells in a StringGrid


There is a way to tell Delphi not to refresh for a time, and then begin
again later.

It goes something like

SuspendRefresh;

For x:= ...
    For y:= ...

EnableRefresh;

The big delays are when Windows is trying to repaint each cell.  I'll look
it up later tonight if someone else doesn't post it first.

Quote
Stefan Svensson wrote:
> I'm using a StringGrid, existing of 40 columns and 1000 rows.
> It works fine, except when I'm clearing all cells. It takes to
> long time.
> The only way I have found out to clear all cells are by clearing cell by
> cell.

> I do this in a loop as following example:

>  for Row := 0 to 999
>           for Col := 0 to 39 do
>             StringGrid.Cells[ACol, ARow] := '';

> Does anyone know a faster way to clear all cells in a StringGrid ?

> Regards
> Stefan

--
Bryan Valencia
Software Services
http://www.209software.com
mailto:br...@209software.com

- "the gravest lie about Y2K matters, is that your company can,
through the acquisition of affidavits of compliance, protect
itself against harm, whether real or litigated. It can't.

This faith in legal documents is hollow and in fact dangerous.
The wisest course of action is for you to immediately disabuse
yourself of this deceit."

- from http://language.perl.com/news/y2k.html

Re:Clearing all cells in a StringGrid


maybe this is what you're pseudo-coding???...

LockWindowUpdate(Screen.ActiveCustomForm.Handle);
try
  .. your code
finally
  LockWindowUpdate(0);
end;

Wayne

Quote
Bryan Valencia wrote:

> There is a way to tell Delphi not to refresh for a time, and then begin
> again later.

> It goes something like

> SuspendRefresh;

> For x:= ...
>     For y:= ...

> EnableRefresh;

> The big delays are when Windows is trying to repaint each cell.  I'll look
> it up later tonight if someone else doesn't post it first.

> Stefan Svensson wrote:

> > I'm using a StringGrid, existing of 40 columns and 1000 rows.
> > It works fine, except when I'm clearing all cells. It takes to
> > long time.
> > The only way I have found out to clear all cells are by clearing cell by
> > cell.

> > I do this in a loop as following example:

> >  for Row := 0 to 999
> >           for Col := 0 to 39 do
> >             StringGrid.Cells[ACol, ARow] := '';

> > Does anyone know a faster way to clear all cells in a StringGrid ?

> > Regards
> > Stefan

> --
> Bryan Valencia
> Software Services
> http://www.209software.com
> mailto:br...@209software.com

> - "the gravest lie about Y2K matters, is that your company can,
> through the acquisition of affidavits of compliance, protect
> itself against harm, whether real or litigated. It can't.

> This faith in legal documents is hollow and in fact dangerous.
> The wisest course of action is for you to immediately disabuse
> yourself of this deceit."

> - from http://language.perl.com/news/y2k.html

Re:Clearing all cells in a StringGrid


Quote
> There is a way to tell Delphi not to refresh for a time, and then begin
> again later.

Yes, you can use something like

  stringgrid.Rows[0].BeginUpdate;
  try

Quote
> For x:= ...
>     For y:= ...

  finally
    stringgrid.Rows[0].EndUpdate;
  end;  

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!

Re:Clearing all cells in a StringGrid


Yes, That's it!  I was suffering from drain bramage, and I couldn't remember....
Quote
Wayne Sheffield wrote:
> maybe this is what you're pseudo-coding???...

> LockWindowUpdate(Screen.ActiveCustomForm.Handle);
> try
>   .. your code
> finally
>   LockWindowUpdate(0);
> end;

> Wayne

Other Threads