Recent

Author Topic: Add new empty row in string grid  (Read 5408 times)

hierophant

  • New member
  • *
  • Posts: 7
Add new empty row in string grid
« on: September 13, 2019, 07:37:45 am »
 8) In Delphi days, after first loading a CSV file into a stringgrid, to add a single new row at the bottom of the grid with all cells empty, after making the grid editable, ISTR (at 87 yrs old) the code was:
Stringgrid1.rowcount := Stringgrid1.rowcount + 1;
This doesn't work in my today's Lazarus effort.  I've RTFM for days. Please tell me what code entry would work.  Apologies in advance for brain seizure.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Add new empty row in string grid
« Reply #1 on: September 13, 2019, 08:41:10 am »
Does your grid's Options include goAutoAddRows?

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Add new empty row in string grid
« Reply #2 on: September 13, 2019, 08:49:52 am »
it's still working this way. Please create a sample and post this in this topic.

Which version Lazarus? Using Windows/ linux / mac?

@howard.
In my example the option is off. Putting the option on is still putting extra rows into the grid.
« Last Edit: September 13, 2019, 08:54:14 am by mangakissa »
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

hierophant

  • New member
  • *
  • Posts: 7
Re: Add new empty row in string grid
« Reply #3 on: September 13, 2019, 09:38:32 am »
Does your grid's Options include goAutoAddRows?
Thank you. It didn't but I've now added it in FormCreate.  It compiles but when run, it brings up an error messagebox 'project daldata raised exception class 'External: SIGSEGV at address 10007A066'.  I'm using Windows 10.

hierophant

  • New member
  • *
  • Posts: 7
Re: Add new empty row in string grid
« Reply #4 on: September 13, 2019, 10:00:28 am »
it's still working this way. Please create a sample and post this in this topic.

Which version Lazarus? Using Windows/ linux / mac?

@howard.
In my example the option is off. Putting the option on is still putting extra rows into the grid.
Thanks. Using Windows10. I have a button named 'addnewrow'. Code:
procedure TForm1.AddNewRow(Sender: TObject);

begin

 SGrid.RowCount :=SGrid.rowcount+1;
end;

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Add new empty row in string grid
« Reply #5 on: September 13, 2019, 10:11:39 am »
The attached project demonstrates that both calling "Grid.RowCount := Grid.RowCount+1" and the gdAutoAddRows option are working as expected.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Add new empty row in string grid
« Reply #6 on: September 13, 2019, 06:01:27 pm »
I see in your original post that a comment was made about establishing the editor mode.

 I Know a few things can go wrong if you don't first test to ensure the editor is present !

 If Editor <> Nil Then Begin Editor.Enable := true; Editor.Visible := true; End;

 And so on.

 I also noted that of you don't have the goEdit set in the options the grid does not place a valid editor there, maybe it should have a valid editor just incase one wants to simply manually turn it on?

P.S.
 If you use the EditorMode := True; instead

 In my Delphi install the cell will highlight regardless if you are in goEditing or not, it puts the CELL into focus, although it follows the goediting rules but none the less, it does put it into focus which I think is the correct way to do this.
 The LCL just does not do this at all..

« Last Edit: September 13, 2019, 06:28:41 pm by jamie »
The only true wisdom is knowing you know nothing

hierophant

  • New member
  • *
  • Posts: 7
Re: Add new empty row in string grid
« Reply #7 on: September 16, 2019, 08:16:48 pm »
The attached project demonstrates that both calling "Grid.RowCount := Grid.RowCount+1" and the gdAutoAddRows option are working as expected.
I am much obliged by the thoughtful message above and also that from jamie. My current .pas coding as a result of trying and failing both for days and reading as many comments as I can find are below:

After making sure that the Inspector also showed True for editing
Code: [Select]
procedure TForm1.FormActivate(Sender: TObject);
begin
  SGrid.loadfromCSVfile('C:\LazProjects\daldataCSV.txt',',',True);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FormActivate(sender);
  SGrid.options := SGrid.options +  [goediting];
  SGrid.Options := SGrid.options + [goalwaysshoweditor];
  SGrid.autoadjustcolumns;
  SGrid.options := SGrid.options + [goautoaddrows];
  SGrid.Rowcount := Sgrid.RowCount+1;
end;
end.
[\code]
     This compiles without problems and runs.  The SGrid options have been added and removed
with the same result: The grid loaded and expanded properly but the grid did not show a blank
 line after the last (Z) entry of the displayed CSV file.  I am as much in the dark as possible
.  I suspect I must have missed something but haven't a clue what it might be.  Any more help
gratefully received.
« Last Edit: September 17, 2019, 07:09:32 pm by hierophant »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Add new empty row in string grid
« Reply #8 on: September 16, 2019, 09:03:55 pm »
I suspect that maybe there is improper termination of the CVS file and when adding it maybe confused.

 Can you do this but not load the file, just add the Rows and see if an error happens?
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Add new empty row in string grid
« Reply #9 on: September 16, 2019, 11:50:33 pm »
I took your code, simplified it, and put it into a small project. Using a dummy csv file it is working as expected. Does it work correctly for you, too?

Please note that the AutoAddRows option has an effect only when you select a cell in the last row and press "Arrow-down". And when you select an old existing cell without entering anything into the new line, the empty line is removed again. And you can add empty lines only one by one.

There is also an option goAutoAddRowsSkipContentCheck in which the newly added line is persistent even if you move the cursor out of the new line. And when you press "Arrow-down" in the empty line another empty line is added.

When you want to have the empty line in the grid all the time you must write some code: Write a procedure which checks whether the last row is already empty. If not increment the grid's RowCount by 1. Call this procedure for example in the OnEditingDone event when the user might have filled the first cell in the last row -- this automatically adds another empty row at the end. In the attached demo you can activate this behavior by checking the "Empty row always visible" button.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Add new empty row in string grid
« Reply #10 on: September 17, 2019, 08:22:37 am »
I haven't read your code wp, but hierophant is putting this line   SGrid.Rowcount := Sgrid.RowCount+1; in formcreate and then he reads the csv into the grid. That's why there's is no blank row after loading the csv.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

hierophant

  • New member
  • *
  • Posts: 7
Re: Add new empty row in string grid
« Reply #11 on: September 17, 2019, 07:27:41 pm »
I haven't read your code wp, but hierophant is putting this line   SGrid.Rowcount := Sgrid.RowCount+1; in formcreate and then he reads the csv into the grid. That's why there's is no blank row after loading the csv.
Hanging my head in shame!  Moving the line from Create to Activate of course did the trick.  My apologies to all for wasting your time.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Add new empty row in string grid
« Reply #12 on: September 18, 2019, 12:41:41 am »
That was a good observation by @mangakissa but he was loading the file first  if you look at his code, he was making a direct call to the FormActivate(Sender), which would load the file, then he followed through with the rest to get extra rows, however, when the OnCreate was completed, the OnFormActivate was called once again thus reloading the grid and not performing any steps to add the extra rows.

 So this was a case of double loads. IF this was the way it worked in Delphi I would like to know how that worked?

The only true wisdom is knowing you know nothing

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Add new empty row in string grid
« Reply #13 on: September 18, 2019, 01:19:45 am »
I haven't read your code wp, but hierophant is putting this line   SGrid.Rowcount := Sgrid.RowCount+1; in formcreate and then he reads the csv into the grid. That's why there's is no blank row after loading the csv.
Hanging my head in shame!  Moving the line from Create to Activate of course did the trick.  My apologies to all for wasting your time.
It's never a waste of time!
We all learn something from all discussions.

 

TinyPortal © 2005-2018