Recent

Author Topic: [SOLVED] StringGrid.LoadFromCSVStream return blank  (Read 1465 times)

dadycool

  • Newbie
  • Posts: 2
[SOLVED] StringGrid.LoadFromCSVStream return blank
« on: September 29, 2021, 07:31:18 am »
Dear Expert

I have requirement to save and load StringGrid from CSV stream using code below:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   st: TStringStream;
  4. begin
  5.  
  6.   {
  7.   //Load from file example
  8.   StringGrid1.SaveToCSVFile('.\test.csv');
  9.   StringGrid1.Clean;
  10.   ShowMessage('Cleanup and Restore');
  11.   StringGrid1.LoadFromCSVFile('.\test.csv');
  12.   }
  13.  
  14.   //Load from stream example
  15.   st:= TStringStream.Create;
  16.   StringGrid1.SaveToCSVStream(st);
  17.   ShowMessage(st.DataString);
  18.   StringGrid1.Clean;
  19.   StringGrid1.LoadFromCSVStream(st);
  20.  
  21.   st.Free;
  22.  
  23. end;
  24.  
  25.  

However my StringGrid show nothing, without even a gridlines. Interestingly save/load from CSV file does work like a charm.

What did I do wrong?  :-\
« Last Edit: September 29, 2021, 10:36:50 am by dadycool »

Thaddy

  • Hero Member
  • *****
  • Posts: 10991
Re: StringGrid.LoadFromCSVStream return blank
« Reply #1 on: September 29, 2021, 07:54:17 am »
Looks like file and stream have different separators. (although IIR the File one uses the stream tooo.
The average programmer productivity is 4-5 hours per day. Peak performance 72 hours for short bursts. MTBF is 1 second or less.

sstvmaster

  • Sr. Member
  • ****
  • Posts: 266
Re: StringGrid.LoadFromCSVStream return blank
« Reply #2 on: September 29, 2021, 09:18:26 am »
You must insert:
Code: Pascal  [Select][+][-]
  1. st.Position:=0;

Code: Pascal  [Select][+][-]
  1.   //Load from stream example
  2.   st:= TStringStream.Create;
  3.   StringGrid1.SaveToCSVStream(st);
  4.   ShowMessage(st.DataString);
  5.   StringGrid1.Clean;
  6.   st.Position:=0; // <--
  7.   StringGrid1.LoadFromCSVStream(st);
  8.  
  9.   st.Free;
  10.  

Because, the position of the stream after saving is at the end of the stream. And when you try to read from the stream again it will read from the end of the stream. But there is nothing to read, so it is empty.

You must set the position to Zero (0) after saving a stream.
Windows 10
Lazarus: 2.0.12 and trunk (main)

dadycool

  • Newbie
  • Posts: 2
Re: StringGrid.LoadFromCSVStream return blank
« Reply #3 on: September 29, 2021, 10:36:29 am »
You must insert:
Code: Pascal  [Select][+][-]
  1. st.Position:=0;

Code: Pascal  [Select][+][-]
  1.   //Load from stream example
  2.   st:= TStringStream.Create;
  3.   StringGrid1.SaveToCSVStream(st);
  4.   ShowMessage(st.DataString);
  5.   StringGrid1.Clean;
  6.   st.Position:=0; // <--
  7.   StringGrid1.LoadFromCSVStream(st);
  8.  
  9.   st.Free;
  10.  

Because, the position of the stream after saving is at the end of the stream. And when you try to read from the stream again it will read from the end of the stream. But there is nothing to read, so it is empty.

You must set the position to Zero (0) after saving a stream.

Wow! I feel like winning a jackpot at this moment!

Thanks so much , sstvmaster. That solved my problem. I pray for your happiness and prosper throughout the year  :D

 

TinyPortal © 2005-2018