Recent

Author Topic: Stringgrid InsertRowWithValues: auto add columns?  (Read 7154 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Stringgrid InsertRowWithValues: auto add columns?
« on: October 25, 2014, 04:52:27 pm »
I have an empty stringgrid (no columns, no rows).
I'd like to insert a row with two columns.

I hit on InsertRowWithValues which looks like an easy way to insert data without having to add rows yourself...

When I try this:
Code: [Select]
  MappingGrid.InsertRowWithValues(0,['test1','dest1']);
I get a list index (-1) out of bounds error.

So... perhaps the code does not insert columns? Adding
Code: [Select]
  MappingGrid.ColCount:=2;
does fix the problem.

Would it make sense to submit a feature request for InsertRowWithValues to either
1. add the required columns itself or
2. provide a more meaningful error message (x column values given while the grid only has y columns)?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1227
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #1 on: October 25, 2014, 04:56:57 pm »
My vote is for the second option.

Nice find,  I never knew this existed.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #2 on: October 25, 2014, 05:22:51 pm »
Mmm, of course, when I tried to write a simple application to reproduce the problem... I didn't get the error.
Didn't get any data either, just apparently empty rows (0 columns? who knows)
Will investigate further when I feel like it :)

Any interested party: test program attached.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Bart

  • Hero Member
  • *****
  • Posts: 3360
    • Bart en Mariska's Webstek
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #3 on: October 25, 2014, 05:31:54 pm »
Please submit a bugreport, you can assign it to me.

Bart

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #4 on: October 25, 2014, 07:03:27 pm »
Ok: issue 26943
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Bart

  • Hero Member
  • *****
  • Posts: 3360
    • Bart en Mariska's Webstek
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #5 on: October 26, 2014, 04:22:06 pm »
Possible patch attached to bugreport.
Jesus should review it.

Bart

chrnobel

  • Full Member
  • ***
  • Posts: 241
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #6 on: November 07, 2018, 05:25:06 pm »
Hi.

I know this is an old topic, but I just had some similar problems with InsertRowWithValues.

The colums were defined, but stil I was not able to insert a row if the rowcount was zero.

I fixed it this way:

if stringgrid1.RowCount=0 then stringgrid1.RowCount:=1;

Bart

  • Hero Member
  • *****
  • Posts: 3360
    • Bart en Mariska's Webstek
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #7 on: November 07, 2018, 06:58:38 pm »
This works fine for me (trunk):

Code: Pascal  [Select]
  1. procedure TForm1.Button6Click(Sender: TObject);
  2. begin
  3.   SGrid.RowCount := 0;
  4.   SGrid.InsertRowWithValues(0,['1','2','3'])
  5. end;

Bart

chrnobel

  • Full Member
  • ***
  • Posts: 241
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #8 on: December 15, 2018, 02:46:42 pm »
Arghh, crap editor, never use tab inside the edit field, then everything is lost!

Trying again.

I have discovered, that if one are using InsertRowWithValues(0,[blah,blah]) then an extra empty row is added to the end of the grid, meaning the row count will be one to high.

I have made the following workaround:

Code: Pascal  [Select]
  1. procedure TMainpageform.additem(Sender: TObject);
  2. var
  3.   zeroline:boolean;
  4. begin
  5.   zeroline:=false;
  6.   if stringgrid1.RowCount=0 then
  7.   begin
  8.      zeroline:=true;
  9.      stringgrid1.RowCount:=1;
  10.    end;
  11.    stringgrid1.InsertRowWithValues(1,[blah,blah]);
  12.    if zeroline then StringGrid1.DeleteRow(0);
  13. end;
  14.  

jamie

  • Hero Member
  • *****
  • Posts: 1282
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #9 on: December 15, 2018, 05:51:34 pm »
I suppose the insert function should be modified to test against the current count?

if count < Requested row+1 Then Add should be called internally,..

Or you the coder could do that?


wp

  • Hero Member
  • *****
  • Posts: 5455
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #10 on: December 15, 2018, 06:19:25 pm »
I have discovered, that if one are using InsertRowWithValues(0,[blah,blah]) then an extra empty row is added to the end of the grid, meaning the row count will be one to high.
Isn't this the way it should be? When I "insert" a card into a stack of cards the number of cards will grow by 1.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jamie

  • Hero Member
  • *****
  • Posts: 1282
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #11 on: December 15, 2018, 07:19:14 pm »
I just tried it,,,
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   StringGrid1.InsertRowWithValues(0,['10','20']);
  4. end;                  
  5.  

Grid has 0 rows in it to start with..
two columns.
seems to work for me.?

chrnobel

  • Full Member
  • ***
  • Posts: 241
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #12 on: December 16, 2018, 02:30:27 pm »
Grid has 0 rows in it to start with..
Does it really have zero rows, or do you have a fixed row - that makes a difference.

chrnobel

  • Full Member
  • ***
  • Posts: 241
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #13 on: December 16, 2018, 02:34:41 pm »
Isn't this the way it should be? When I "insert" a card into a stack of cards the number of cards will grow by 1.
Yes, but that is not the way it works.

If I at the beginning are having zero rows, and I insert what is supposed to be one and only one row as the first (0), I end up with having two rows in the grid - the first (row 0) with data, and an empty row after that (row 1).

Handoko

  • Hero Member
  • *****
  • Posts: 2900
  • My goal: build my own game engine using Lazarus
Re: Stringgrid InsertRowWithValues: auto add columns?
« Reply #14 on: December 16, 2018, 02:47:49 pm »
Maybe we should check how TStringGrid behaves in Delphi. Although I'm not a fan of Delphi but ... you know ... maintaining compatibility with Delphi is important.