Bookstore

Recent

Author Topic: What is the best way to save the contents of a StringGrid?  (Read 548 times)

Matt723

  • New Member
  • *
  • Posts: 20
What is the best way to save the contents of a StringGrid?
« on: January 24, 2020, 06:12:42 pm »
What is the best way to save the contents of the StringGrid every time the program is closed?

Handoko

  • Hero Member
  • *****
  • Posts: 3434
  • My goal: build my own game engine using Lazarus
Re: What is the best way to save the contents of a StringGrid?
« Reply #1 on: January 24, 2020, 06:36:23 pm »
You can save the data as a text file, ini/config file, xml, database or (Windows) registry.

What is the best depends on the complexity and size of the data, and what method is most convenient to you.

Text File tutorial:
https://wiki.freepascal.org/Text

XML tutorial:
https://wiki.freepascal.org/XML_Tutorial

Ini File tutorial:
https://wiki.freepascal.org/Using_INI_Files

Windows Registry tutorial:
https://wiki.freepascal.org/fcl-registry

Tdbf tutorial:
https://wiki.lazarus.freepascal.org/Lazarus_Tdbf_Tutorial

And this is my demo showing how to load data from a dbf file to a string grid:
https://forum.lazarus.freepascal.org/index.php/topic,37181.msg249361.html#msg249361
« Last Edit: January 24, 2020, 06:40:03 pm by Handoko »

Awkward

  • Jr. Member
  • **
  • Posts: 53
Re: What is the best way to save the contents of a StringGrid?
« Reply #2 on: January 24, 2020, 06:53:16 pm »
why not to use default ability? these methods:
    procedure LoadFromCSVStream(AStream: TStream; ADelimiter: Char=',';
      UseTitles: boolean=true; FromLine: Integer=0; SkipEmptyLines: Boolean=true); virtual;
    procedure LoadFromCSVFile(AFilename: string; ADelimiter: Char=',';
      UseTitles: boolean=true; FromLine: Integer=0; SkipEmptyLines: Boolean=true);
    procedure SaveToCSVStream(AStream: TStream; ADelimiter: Char=',';
      WriteTitles: boolean=true; VisibleColumnsOnly: boolean=false);
    procedure SaveToCSVFile(AFileName: string; ADelimiter: Char=',';
      WriteTitles: boolean=true; VisibleColumnsOnly: boolean=false);

Handoko

  • Hero Member
  • *****
  • Posts: 3434
  • My goal: build my own game engine using Lazarus
Re: What is the best way to save the contents of a StringGrid?
« Reply #3 on: January 24, 2020, 06:54:46 pm »
Yes, I forgot about it.  :D

wp

  • Hero Member
  • *****
  • Posts: 6824
Re: What is the best way to save the contents of a StringGrid?
« Reply #4 on: January 24, 2020, 07:47:58 pm »
The CSV-related procedures save/load only the cell content. If you also want to handle grid layout (col count, row count, colwidths, ...), focused cell, colors (when working with columns) etc you should use SaveToFile and LoadFromFile. There is a chapter in the grids wiki about it: https://wiki.lazarus.freepascal.org/Grids_Reference_Page#Save_and_Retrieve_Grid_Content

See also attached demo.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10