🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1687

Re: Best way to clear a stringgrid?

#1687
From: Dan
Date: Fri, 29 Jun 2007 18:52
88 lines
3975 bytes
> That is not true.  All rows/columns that are removed are freed from
> memory.  There is nothing physically available for the TStringGrid to
> keep displaying for them.
> 
> Try this:
> 
>     void ClearStringGrid(TStringGrid* sgGrid)
>     {
>         sgGrid->RowCount = (sgGrid->FixedRows+1);
>         sgGrid->Rows[sgGrid->FixedRows]->Clear();
>     }
> 

The above doesn't work. Indeed it creates the image that the gridis
cleared and it 'looks' like everything is gone, but simply set the
Row/Column count back again and you will see that the strings that were
entered into the grid are still there.

For example, creating some basic test code like the following including
three buttons aptly named "ResizeGridButton", "FillGridButton", and
"ClearGridButton" and one stringgrid named StringGrid1, and you will see
what I mean when you assign the appropraite events to each:

//---------------------------------------------------------------------------
void __fastcall TForm1::ResizeGridButtonClick(TObject *Sender)
{
   StringGrid1->RowCount = 10;
   StringGrid1->ColCount=10;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FillGridButtonClick(TObject *Sender)
{
    ResizeGridButtonClick(this);
    for (int i=0; i<10;i++)
    {
      for (int j=0; j<10; j++)
      {
        StringGrid1->Cells[i][j] = i+j;
      }
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClearGridClick(TObject *Sender)
{
ClearStringGrid(StringGrid1);
}
//---------------------------------------------------------------------------

In the GUI, click the FillGrid button, then the ClearGrid button, then
the ResizeGrid button and you will notice that even though the 'clear
grid' button cleared the grid visually, when you resize it you will see
that a large number of values still exist.

Based on the results from above, I did modify the ClearStringGrid code
to the following:
//---------------------------------------------------------------------------
void ClearStringGrid(TStringGrid* sgGrid)
{
    if (sgGrid->RowCount < sgGrid->ColCount)
    {
     //clear all the rows if there are less rows than columns
      for (int i=0; i<sgGrid->RowCount; i++)
      {
        sgGrid->Rows[i]->Clear();
      }
    }
    else
    {
      //clear all the columns if there are less columns than rows
      for (int i=0; i<sgGrid->ColCount; i++)
      {
        sgGrid->Cols[i]->Clear();
      }
    }

    sgGrid->ColCount=1;
    sgGrid->RowCount=1;
}


Even the above though takes nearly 10 seconds to clear a Grid containing
  275K rows and only 4 columns on my 2GHz DUO-Core WinXP system with 2GB
of RAM.  Yes, it's faster than the original code, but I expect faster.
Am I wrong to expect that this clearing process should be possible in
less than a second?

Dan
Technical Details
Message-ID: <468554ee$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4683bbbe$1@newsgroups.borland.com> <4683e2e7$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,dbe415a5e9add767
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Fri, 29 Jun 2007 13:52:30 -0500
From: Dan <Dan@do.not.spam>
User-Agent: Thunderbird 1.5.0.12 (Windows/20070509)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Best way to clear a stringgrid?
References: <4683bbbe$1@newsgroups.borland.com> <4683e2e7$1@newsgroups.borland.com>
In-Reply-To: <4683e2e7$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 216.174.165.174
Message-ID: <468554ee$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1183143150 216.174.165.174 (29 Jun 2007 11:52:30 -0700)
Lines: 90
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail