Solved

Clear all rows in a TStringGrid ??

Posted on 1999-01-15
15
1,097 Views
Last Modified: 2008-03-17
How can i clear some or all rows in a tstringgrid.

I dont want to use the command 'stringgrid1.rows[xx].clear'.
0
Comment
Question by:PeterLarsen
  • 5
  • 4
  • 2
  • +2
15 Comments
 

Expert Comment

by:Tulip
ID: 1356786
I don't see another solution then this one:

for i := 0 to StringGrid1.RowCount-1 do StringGrid1.Rows[i].Clear;

But I don't use StringGrid. There is a better one available, called HyperGrid.
There you can call  HyperGrid1.Clear or even define an area (from..to) to clear.

You can find it at:   http://www.pablop.demon.co.uk/


Cheers, Tulip
0
 
LVL 3

Expert Comment

by:williams2
ID: 1356787
Their is severel ways to do this

1) You might clear the actual cell texts
2) Set the StringGrid1.RowCount to minimum
3) Set the StringGrid1.ColCount to minimum

To clear all the cell properties do like:

For i:= 0 to StringGrid1.RowCount-1 do
  For j:= 0 to StringGrid1.ColCount-1 do
    StringGrid1.Cells[j,i]:= '';

Regards,
Williams
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1356788
Hmmm. Why not?
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 20

Expert Comment

by:Madshi
ID: 1356789
I meant, why don't you want to use 'stringgrid1.rows[xx].clear'?
0
 

Expert Comment

by:Tulip
ID: 1356790
If you have problems with that URL (actually I have them at the moment) then you
can try it at Torry's Delphi Pages:

http://www.torry.ru/vcl/grids/hyperd2.zip  (Delphi 2)
http://www.torry.ru/vcl/grids/hyperd3.zip  (Delphi 3)
http://www.torry.ru/vcl/grids/hyperd4.zip  (Delphi 4)

Cheers, Tulip
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 1356791
First, thanks to all. I posted my question 5 min ago...

Tulip : I must use standard components in this project.

William : I want to clear 500000 rows (sometime more). By using 'stringgrid.rows[xx].clear' -it takes too long time to clear so many rows.

Regards
PL
0
 

Expert Comment

by:Tulip
ID: 1356792
Well, then destroy grid and create a new one .
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 1356793
First, thanks to all. I posted my question 5 min ago...

Tulip : I must use standard components in this project.

William : I want to clear 500000 rows (sometime more). By using 'stringgrid.rows[xx].clear' -it takes too long time to clear so many rows.

Regards
PL
0
 

Expert Comment

by:Tulip
ID: 1356794
Well, then destroy the grid and create a new one. That's brute but fast.

Tulip
0
 

Expert Comment

by:Tulip
ID: 1356795
Well, then destroy the grid and create a new one. That's brute but fast.

Tulip
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 50 total points
ID: 1356796
I think the solution is not to clear the grid at all if you are dealing with 500,000 rows or more. Treat the grid as a virtual display: ie: set the rowcount of the grid to 500,000 or whatever the number is, but never actually populate the grid cells with strings.

To display strings in the cells override the DrawCell event, like this:

Function MyDataSource(Row, Col : Integer) : String;
begin
  Result := 'Fred'; // You would set it to your required string
end;

procedure TForm3.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  StringGrid1.canvas.TextOut(Rect.left, rect.bottom, MyDataSource(ACol, ARow));
end;

Also, use the GetEditText and SetEditText events to permit the user to edit the data in the cells.

In this way you never have to clear the grid and you save all that time populating it in the first place.

Cheers,

Raymond.

0
 
LVL 3

Expert Comment

by:williams2
ID: 1356797
I would say that you'll have to make a custom handing more like having your own datastructure build together with an ownerdrawn grid

Regards,
Williams
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 1356798
Hi Raymond and thanks..

Your idea is fine but.... The application-stringgrid reads its data from either a local CDRom or the Internet.
When reading from the Internet, the data has to be stored somewhere in the system (a type of Record or TstringList), otherwise the system is getting very slow. Its takes up memory - each row (4 byte) + each record (?? byte).
I am using OnDrawCell to draw colors others than windows standard colors - so this part i easy.

Your idea may be the only way to do this - i don't know. It takes some re-programming to emplement your idea.
It may also slow down the system to dynamic create+destroy the so many records.

regards, PL
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1356799
Well, you have to read the information anyway, the only difference is instead of injecting it into the string grid you would store it on disk, or perhaps create a TList descendent that stored objects, each of which stored the information for a row (or you could even use a DynamicArray).

The options are certainly there, though I don't think it would be a lot of work to change it.

Cheers,

Raymond.
0
 
LVL 2

Author Comment

by:PeterLarsen
ID: 1356800
Well, there are more than 20.000 lines of code in 25 units. I may have to change 10-20 % of the code. :(
But - sometimes you have to change a lot - to make it perfect.

Regards, PL.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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