How to export only filled rows from a TStringGrid? lazarus / 31-LCL ---------------------------------------------------------------------- edvard · 2015-04-26 04:17 OK, another TStringGrid question.   I guess the easy answer is to only have as many rows as entries by starting with one and adding as needed, but it's not always the best way.  SO... I have a TStringGrid which I start out with 12 rows, but I realize not all users will fill all 12 rows.  When I export to CSV, it exports the extra lines as lines of commas at the end of the information.  Very messy.  I'd like to export only the filled rows (my program keeps an index of the last row filled), is there an easy way to do that?  I've been over the docs and options and I can't find any way to pass only the filled rows to SaveToCSVFile.  Any suggestions? P.S. Also, how to get the index of the last row imported when doing a LoadFromCSVFile? ---------------------------------------------------------------------- Bart · 2015-04-26 08:49 You'll have to do it manually I'm afraid. A workaround woul be to create another instane of TSringGrid and copy only filled columns to that one. Then let the newly created grid do the SaveToCSVFile. You could also post a feature request in the bugtracker for an extra optional parameter (e.g. SkipEmptyRows). Bart ---------------------------------------------------------------------- Fripsy · 2015-04-26 09:02 Or you could itterate over the grid and put all the filled lines on top, then afterwards set the rowcount property to the number of filled lines... ---------------------------------------------------------------------- howardpc · 2015-04-26 14:36 I think this procedure does what you are after: Code: [Select]procedure SaveNonBlankRowsToFile(aGrid: TStringGrid; const aFilename: string); var   tmp: TStringList;   r: integer;   function IsBlank(aStrings: TStrings): boolean;   var     s: string;   begin     Result:=True;     for s in aStrings do      if (s <> '') then        Exit(False);   end; begin   tmp:=TStringList.Create;   try     for r:=0 to aGrid.RowCount-1 do      if not IsBlank(aGrid.Rows[r]) then        tmp.Add(aGrid.Rows[r].CommaText);     if (tmp.Count > 0) then       tmp.SaveToFile(aFilename);   finally     tmp.Free;   end; end; ---------------------------------------------------------------------- edvard · 2015-04-28 03:21 Quote from: Bart on April 26, 2015, 10:49:50 am... A workaround would be to create another instance of TSringGrid and copy only filled columns to that one. Then let the newly created grid do the SaveToCSVFile. I got the same suggestion from a programmer friend who uses C++ builder a lot, so he knew what I was talking about when I said "TStringGrid".  I'll keep that in my bag of tricks... QuoteYou could also post a feature request in the bugtracker for an extra optional parameter (e.g. SkipEmptyRows). I think that's a pretty good idea.  I'll do it. Quote from: Fripsy on April 26, 2015, 11:02:25 amOr you could itterate over the grid and put all the filled lines on top, then afterwards set the rowcount property to the number of filled lines... All lines will be filled from the top row to the last row used, there won't be any intermediate blanks, so it will be just as effective to simply do a loop that detects filled rows, and set .RowCount to the index of the last filled row detected.  Very simple and direct, I like it! Quote from: howardpc on April 26, 2015, 04:36:04 pmI think this procedure does what you are after: ... Ahh... example code; a thousand thank you's, Howard! I'll be trying all of these out tonight, thank you everyone! For now, I just have the TStringGrid start out with 1 row and create new rows as I need them.  Having the blank space below the grids doesn't look as bad as I thought it would, but I'll ask my potential users what they'd prefer. Thanks so much everybody!