Article View: borland.public.cppbuilder.nativeapi
Article #1684Re: Best way to clear a stringgrid?
From: Dan
Date: Fri, 29 Jun 2007 17:55
Date: Fri, 29 Jun 2007 17:55
88 lines
3930 bytes
3930 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:
<46854789.701@do.not.spam>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<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 Message-ID: <46854789.701@do.not.spam> Date: Fri, 29 Jun 2007 12:55:21 -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 To: "Remy Lebeau (TeamB)" <no.spam@no.spam.com> 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 X-Trace: newsgroups.borland.com 1183139721 216.174.165.174 (29 Jun 2007 10:55:21 -0700) Lines: 89 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail