Recent

Author Topic: StringGrid Clear? (solved!!!)  (Read 1638 times)

pentilisea

  • Guest
StringGrid Clear? (solved!!!)
« on: March 11, 2023, 02:49:14 pm »
Hi again,
getting along with StringGrid fine although...

I configured StringGrid1 with the Object Inspector

The StringGrid displays my diverse calculation. All good.

Before loading next file I clear all Edits and so on.

When I do StringGrid1.Clear, it clears fine, but I can't
restore the original settings I did with Object Inspector.

The following lines leaves the first row0 (?) with titles ok,
loding new file OK, but it does not write again the results into
Row 1 - etc...

Code: Pascal  [Select][+][-]
  1. for c := spins downto 0 do begin
  2.   StringGrid1.DeleteRow(1);
  3.   StringGrid1.Refresh;
  4.   end;

The Grid is configured to have 100 rows to start with.

Well, how do I clear,clean or empty the written rows and start
again with the original code? (which of course fails after a StringGrid1.Clear event)

Code: Pascal  [Select][+][-]
  1.    StringGrid1.Cells[1, spins] := Edit1.text;
  2.    StringGrid1.Cells[2, spins] := inttostr(spins);
  3.    StringGrid1.Cells[3, spins] := IntToStr(beton);
  4.    StringGrid1.Cells[4, spins] := IntToStr(wager);
  5.    StringGrid1.Cells[5, spins] := IntToStr(loss);
  6.    StringGrid1.Cells[6, spins] := IntToStr(win);
  7.    StringGrid1.Cells[7, spins] := IntToStr(budget);
  8.    StringGrid1.Cells[8, spins] := mode;
  9.    StringGrid1.Cells[9, spins] := Label1.Caption;
  10.    StringGrid1.Refresh;

Thanks for your consideration and time.
   For better understanding see attachments.
   
   Klaus     



« Last Edit: March 12, 2023, 07:51:33 pm by pentilisea »

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: StringGrid Clear?
« Reply #1 on: March 11, 2023, 03:14:59 pm »
If I understand correctly, you should look at the CLEAN options in the string grid.

There a few to choose from.
 you can selectively clean out the cells you want and leave all others.
The only true wisdom is knowing you know nothing

dsiders

  • Hero Member
  • *****
  • Posts: 1084
Re: StringGrid Clear?
« Reply #2 on: March 11, 2023, 04:31:02 pm »
If I understand correctly, you should look at the CLEAN options in the string grid.

There a few to choose from.
 you can selectively clean out the cells you want and leave all others.

https://dsiders.gitlab.io/lazdocsnext/lcl/grids/tcustomstringgrid.clean.html
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

pentilisea

  • Guest
Re: StringGrid Clear?
« Reply #3 on: March 11, 2023, 07:33:25 pm »
Thanks,



I wrote 5 rows into the grid.

Did StringGrid1.Clean([]);

It leaves title row intact.

It cleans out the 5 rows OK, but then restarts at row 6, although assignet to write row 1 ...

Sorry I'm not getting the syntax with all the other stuff correctly done.

Klaus

dsiders

  • Hero Member
  • *****
  • Posts: 1084
Re: StringGrid Clear?
« Reply #4 on: March 11, 2023, 08:22:06 pm »
Thanks,

I wrote 5 rows into the grid.

Did StringGrid1.Clean([]);

It leaves title row intact.

It cleans out the 5 rows OK, but then restarts at row 6, although assignet to write row 1 ...

Sorry I'm not getting the syntax with all the other stuff correctly done.

Klaus

If Clear and Clean do not meet your needs, just use RowCount and ColCount to truncate the grid to the size you want.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: StringGrid Clear?
« Reply #5 on: March 11, 2023, 09:48:33 pm »
I still don't understand. You edited some *data* cells in design-time, than you rewrite them and you want to restore them?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: StringGrid Clear?
« Reply #6 on: March 11, 2023, 11:25:16 pm »
He just simply wants a fixed header on top that remains always and just be able to start the grid with no  rows after the fixed header so he doesn't have to redefine them.

  Basically a NEW feature but keeping the existing header definitions in tacked and no dead spaced cells.

 Basically a DeleteRow loop.

That's my take on it.

 
The only true wisdom is knowing you know nothing

dsiders

  • Hero Member
  • *****
  • Posts: 1084
Re: StringGrid Clear?
« Reply #7 on: March 12, 2023, 01:10:23 am »
Basically a NEW feature but keeping the existing header definitions in tacked and no dead spaced cells.

Why a new feature? Just use RowCount.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: StringGrid Clear?
« Reply #8 on: March 12, 2023, 01:15:56 am »
Quote
for c := spins downto 0 do begin
  StringGrid1.DeleteRow(1);
  StringGrid1.Refresh;
  end;

Code: Pascal  [Select][+][-]
  1. For C := StringGrid1.RowCount-1 downto 1 Do DeleteRow(C);

The only true wisdom is knowing you know nothing

dsiders

  • Hero Member
  • *****
  • Posts: 1084
Re: StringGrid Clear?
« Reply #9 on: March 12, 2023, 01:19:04 am »
Quote
for c := spins downto 0 do begin
  StringGrid1.DeleteRow(1);
  StringGrid1.Refresh;
  end;

Code: Pascal  [Select][+][-]
  1. For C := StringGrid1.RowCount-1 downto 1 Do DeleteRow(C);

Code: Pascal  [Select][+][-]
  1. StringGrid1.RowCount := StringGRid1.FixedRows;
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

pentilisea

  • Guest
Re: StringGrid Clear?
« Reply #10 on: March 12, 2023, 10:16:14 am »
Hi Guys,
 as an old grumpy guy being slow and not at home with logic,
 the discussion is quite impressive. Thanks for venting my case.
 
 I agree rowcount maybe a solution.
 
 As said, with the object inspector I assigned 100 rows to start
 with. Actually I should redesign the StringGrid by code after
 deleting rows - would take me a week or so....
 
 So I only assigned 2 rows, 1 for the titles and 1 for the first row to write onto (and add a row for the next writing).
 
 So far so good, deletion works as it should and it also writes
 again - but! only after the deleted rows. I.e. The rows written
 before are staying empty.
 If  I do not assign new rows (+1) error comes with:
      Index Out of range Cell[Col=1 Row=4].
     
 Here my code so far:

Code: Pascal  [Select][+][-]
  1. StringGrid1.RowCount := StringGrid1.RowCount+1;
  2.  
  3.    StringGrid1.Cells[1, spins] := Edit1.text;
  4.    StringGrid1.Cells[2, spins] := inttostr(spins);
  5.    StringGrid1.Cells[3, spins] := IntToStr(beton);
  6.    StringGrid1.Cells[4, spins] := IntToStr(wager);
  7.    StringGrid1.Cells[5, spins] := IntToStr(loss);
  8.    StringGrid1.Cells[6, spins] := IntToStr(win);
  9.    StringGrid1.Cells[7, spins] := IntToStr(budget);
  10.    StringGrid1.Cells[8, spins] := mode;
  11.    StringGrid1.Cells[9, spins] := Label1.Caption;

And the deletion part:

Code: Pascal  [Select][+][-]
  1. For C := StringGrid1.RowCount-1 downto 1 Do
  2.   StringGrid1.DeleteRow(C);
  3.   //StringGrid1.RowCount := StringGRid1.FixedRows;
  4.  
  5.   StringGrid1.RowCount := StringGrid1.ColCount+1;


So, before you start yawning, consider to explain the issues
 to a 4 year old brat.
 
 What am I missing?
 
 Klaus       


bytebites

  • Hero Member
  • *****
  • Posts: 642
Re: StringGrid Clear?
« Reply #11 on: March 12, 2023, 10:57:09 am »
Rowcount := Colcount ?

cdbc

  • Hero Member
  • *****
  • Posts: 1090
    • http://www.cdbc.dk
Re: StringGrid Clear?
« Reply #12 on: March 12, 2023, 11:24:48 am »
Hi
Well dsiders said it:
Code: Pascal  [Select][+][-]
  1. StringGrid1.RowCount := StringGRid1.FixedRows;
You may even get away with just:
Code: Pascal  [Select][+][-]
  1. StringGrid1.RowCount := 1;
In essence Thrash this:
Code: Pascal  [Select][+][-]
  1. For C := StringGrid1.RowCount-1 downto 1 Do
  2.   StringGrid1.DeleteRow(C);
  3.   //StringGrid1.RowCount := StringGRid1.FixedRows;
  4.  
  5.   StringGrid1.RowCount := StringGrid1.ColCount+1;
And do one of the above mentioned!!!
Hth - Regards Benny
« Last Edit: March 12, 2023, 11:31:00 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

alpine

  • Hero Member
  • *****
  • Posts: 1067
Re: StringGrid Clear?
« Reply #13 on: March 12, 2023, 11:30:45 am »
@pentilisea
You must design your header (1-st row) into the form designer.

Then, to clear all other rows after the header you simply write: (as @dsiders said)
Code: Pascal  [Select][+][-]
  1.   StringGrid1.RowCount := StringGrid1.FixedRows;
You don't need to do anything else (at least until you don't keep something additionally allocated in  StringGrid1.Objects[]).

In case you have read a new file and you want to show it into the grid:
Code: Pascal  [Select][+][-]
  1.   StringGrid1.RowCount := StringGrid1.FixedRows + <FileRecordsCount>;
  2.   for C := 0 to <FileRecordsCount>-1 do // Assuming index is 0 based
  3.   begin
  4.      StringGrid1.Cells[1, StringGrid1.FixedRows + C] := FileRecord[C].field1;
  5.      StringGrid1.Cells[2, StringGrid1.FixedRows + C] := IntToStr(FileRecord[C].field2);
  6.      StringGrid1.Cells[3, StringGrid1.FixedRows + C] := <whatever>;
  7.      ... // etc. Just be sure to overwrite all non-fixed cells
  8.   end;
You can replace StringGrid1.FixedRows simply with 1 if you're quite sure the header will be always 1 row.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

pentilisea

  • Guest
Re: StringGrid Clear?
« Reply #14 on: March 12, 2023, 11:33:21 am »
its all about rows

I corrected the mistake since, but it still is complaining about col 0 being out od range.

I added then 3 rows instead of 1 , it works then, but first written rows staying empty.

I will test the last suggestions, but maybe should delete the control and redesign the Grid
with the object inspector all new. Not inserting titles, they will be written by code eacht time  a new line is added?

Thanks again for your attention....

To alpine, I'm not adding files into the grid, only calculated numbers, thouse saved as csv file....

Klaus
« Last Edit: March 12, 2023, 06:26:16 pm by pentilisea »

 

TinyPortal © 2005-2018