Stringgrid InsertRowWithValues: auto add columns? lazarus / 31-LCL ---------------------------------------------------------------------- BigChimp · 2014-10-25 14:52 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)? ---------------------------------------------------------------------- Mike.Cornflake · 2014-10-25 14:56 My vote is for the second option. Nice find,  I never knew this existed. ---------------------------------------------------------------------- BigChimp · 2014-10-25 15:22 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. ---------------------------------------------------------------------- Bart · 2014-10-25 15:31 Please submit a bugreport, you can assign it to me. Bart ---------------------------------------------------------------------- BigChimp · 2014-10-25 17:03 Ok: issue 26943 ---------------------------------------------------------------------- Bart · 2014-10-26 15:22 Possible patch attached to bugreport. Jesus should review it. Bart ---------------------------------------------------------------------- chrnobel · 2018-11-07 16:25 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 · 2018-11-07 17:58 This works fine for me (trunk): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button6Click(Sender: TObject);begin  SGrid.RowCount := 0;  SGrid.InsertRowWithValues(0,['1','2','3'])end; Bart ---------------------------------------------------------------------- chrnobel · 2018-12-15 13:46 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMainpageform.additem(Sender: TObject);var  zeroline:boolean;begin  zeroline:=false;  if stringgrid1.RowCount=0 then  begin     zeroline:=true;     stringgrid1.RowCount:=1;   end;   stringgrid1.InsertRowWithValues(1,[blah,blah]);   if zeroline then StringGrid1.DeleteRow(0);end; ---------------------------------------------------------------------- jamie · 2018-12-15 16:51 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 · 2018-12-15 17:19 Quote from: chrnobel on December 15, 2018, 02:46:42 pmI 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. ---------------------------------------------------------------------- jamie · 2018-12-15 18:19 I just tried it,,, Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  StringGrid1.InsertRowWithValues(0,['10','20']);end;                   Grid has 0 rows in it to start with.. two columns. seems to work for me.? ---------------------------------------------------------------------- chrnobel · 2018-12-16 13:30 Quote from: jamie on December 15, 2018, 07:19:14 pmGrid 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 · 2018-12-16 13:34 Quote from: wp on December 15, 2018, 06:19:25 pmIsn'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 · 2018-12-16 13:47 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. ---------------------------------------------------------------------- wp · 2018-12-16 13:48 I thought that this is due to the special role that the fixed row plays. But for testing I cannot even set RowCount to 0 while FixedRows is 1. When FixedRows=0 and RowCount=0 I cannot reproduce your observation. What is your Lazarus version? Mine is trunk, you should at least try v2RC2 - maybe the bug is fixed there already. ---------------------------------------------------------------------- wp · 2018-12-16 13:51 Quote from: Handoko on December 16, 2018, 02:47:49 pmMaybe 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. Nothing to compare with: Delphi's TStringGrid does not have an InsertRowWithValues... ---------------------------------------------------------------------- Bart · 2018-12-16 13:51 I cannot reporduce this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button6Click(Sender: TObject);begin  Sgrid.RowCount := 0;  SGrid.InsertRowWithValues(0,['1','2','3'])end; This will give me a grid with 1 row, with or without goAlwaysAddRow option. Tested with trunk, but this code has been unchanged for a while now. Bart ---------------------------------------------------------------------- Bart · 2018-12-16 13:52 Quote from: wp on December 16, 2018, 02:48:39 pmI thought that this is due to the special role that the fixed row plays. But for testing I cannot even set RowCount to 0 while FixedRows is 1. This is by design. Bart ---------------------------------------------------------------------- wp · 2018-12-16 13:55 Quote from: Bart on December 16, 2018, 02:51:10 pmI cannot reporduce this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button6Click(Sender: TObject);begin  Sgrid.RowCount := 0;  SGrid.InsertRowWithValues(0,['1','2','3'])end; This will give me a grid with 1 row, with or without goAlwaysAddRow option. Tested with trunk, but this code has been unchanged for a while now. Bart No, this crashes for me with EGridException "FixedRows can't be > RowCount". Just an unchanged StringGrid on a new form. ---------------------------------------------------------------------- Bart · 2018-12-16 14:02 Sorry, I have FixedRows = 0 set in OI, otherwise the crash is as expected... Bart ---------------------------------------------------------------------- Bart · 2018-12-16 14:10 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomStringGrid.InsertRowWithValues(Index: Integer;  Values: array of String);var  i, OldRC: Integer;begin  OldRC := RowCount;  if Length(Values) > ColCount then    ColCount := Length(Values);  InsertColRow(false, Index);  //if RowCount was 0, then setting ColCount restores RowCount (from FGridPropBackup)  //which is unwanted here, so reset it (Issue #0026943)  if (OldRc = 0) then RowCount := 1;  for i := 0 to Length(Values)-1 do    Cells[i, Index] := Values[i];end; It explicitely sets RowCount to 1 if you start out with a RowCount of zero. This was at the time of Lazarus 1.4. Bart ---------------------------------------------------------------------- jamie · 2018-12-16 14:55 For me using 1.8.4 it works as it should however, I wonder what would happen if you try to insert a row with more columns than the grid has ?   Could it be additional columns are being wrapped around to generate an additional row?  I haven't tested this yet. ---------------------------------------------------------------------- Bart · 2018-12-16 15:02 Quote from: jamie on December 16, 2018, 03:55:38 pm..I wonder what would happen if you try to insert a row with more columns than the grid has ? Check the code above, ColCount will be adjusted then... Bart ---------------------------------------------------------------------- chrnobel · 2018-12-16 15:31 Quote from: Handoko on December 16, 2018, 02:47:49 pmMaybe 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. As I see it, it would be better having an AddRowWithValues - that would not break (backward) compatibility with Delphi, but would be a much more intuitive approach. So regardless of whether the grid is totally empty or not, and regardless of fixed rows, it would always append a row in the bottom of the grid. ---------------------------------------------------------------------- Bart · 2018-12-16 16:10 Lazarus StringGrid is not compatible with Delphi's. Not sure how Delphi's is now, but we were way ahead of Delphi with ours, and we're not going to remove everything we have and the don't. According to Embarcadero they don't have AddRow() or InsertRow() (with or without values) at all? Bart ---------------------------------------------------------------------- wp · 2018-12-16 16:18 I checked with Delphi 10.2 Community Edition: Their StringGrad has neither an InsertRowWithValues, nor an AddInsertRowWithValues, nor any other Insert or Add related to rows. ---------------------------------------------------------------------- jamie · 2018-12-16 18:36 Its all done within the COLS/ROWS which are Tstrings where you can insert,add as you do with stringlist. and I think you can also just set the rows/cols and it will also do the same but be blank I stand corrected.. you need to set the ROW count to add a new one.but it can be done in Delphi with access to the main string pool.. Edit2:   I looked at one of my old projects in Delphi, yes I did a hack and wrapped the TStringGrid with another within the app to give me all that good stuff.. Then I noticed that I created a component of that Grid and had installed it on the ToolBar to make it usable via the OI..   Its strange what happens when you get OLD-der