Recent

Author Topic: How to export only filled rows from a TStringGrid?  (Read 3091 times)

edvard

  • Full Member
  • ***
  • Posts: 172
How to export only filled rows from a TStringGrid?
« on: April 26, 2015, 06:17:02 am »
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?
« Last Edit: April 26, 2015, 06:19:52 am by edvard »
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How to export only filled rows from a TStringGrid?
« Reply #1 on: April 26, 2015, 10:49:50 am »
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

  • New Member
  • *
  • Posts: 14
Re: How to export only filled rows from a TStringGrid?
« Reply #2 on: April 26, 2015, 11:02:25 am »
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...
Lazarus 1.4.0 r48774 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to export only filled rows from a TStringGrid?
« Reply #3 on: April 26, 2015, 04:36:04 pm »
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

  • Full Member
  • ***
  • Posts: 172
Re: How to export only filled rows from a TStringGrid?
« Reply #4 on: April 28, 2015, 05:21:34 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...

Quote
You 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.

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...

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!

I 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!
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

 

TinyPortal © 2005-2018