Recent

Author Topic: (Solved)Grids and XML save  (Read 1199 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
(Solved)Grids and XML save
« on: August 26, 2020, 07:25:59 am »
I have copied out the example of saving the contnts of a Grid tp an XML Doc.
It didn't work.
I have attached the program and data XML file.
Could someone take a look ;

Thanks

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes,  SysUtils, LResources, Forms,
  9.   Controls, Graphics, Dialogs,    Grids,
  10.   Buttons,  StdCtrls, XMLCfg;
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     btnGo: TBitBtn;
  17.     OpenDialog1: TOpenDialog;
  18.     StringGrid1: TStringGrid;
  19.     procedure btnGoClick(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. begin
  38.  stringgrid1.SaveOptions := [soDesign,soPosition,soAttributes,soContent];
  39. end;
  40.  
  41. procedure TForm1.btnGoClick(Sender: TObject);
  42. begin
  43.   if opendialog1.Execute then
  44.  begin
  45.    //Clear the grid
  46.    StringGrid1.Clear;
  47.    //Load the XML
  48.    StringGrid1.LoadFromFile(OpenDialog1.FileName);
  49.    //Refresh the Grid
  50.    StringGrid1.Refresh;
  51.  end;
  52. end;
  53.  
  54. end.
  55.  


https://wiki.freepascal.org/Grids_Reference_Page#Using_SaveToCSVFile_and_LoadFromCSV_file
« Last Edit: October 10, 2020, 04:40:19 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Grids and XML save
« Reply #1 on: August 26, 2020, 10:39:05 am »
It didn't work.
Very precise error description...

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Grids and XML save
« Reply #2 on: August 26, 2020, 12:37:34 pm »
If I change your data file content to the following
Code: Pascal  [Select][+][-]
  1. <?xml version="1.0"?>
  2. <CONFIG>
  3.   <grid version="3">
  4.     <saveoptions create="True" position="True" content="True"/>
  5.     <design columncount="2" rowcount="5" fixedcols="1" fixedrows="1" defaultcolwidth="150" defaultRowHeight="22">
  6.       <options>
  7.         <goFixedVertLine value="True"/>
  8.         <goFixedHorzLine value="True"/>
  9.         <goVertLine value="True"/>
  10.         <goHorzLine value="True"/>
  11.         <goRangeSelect value="True"/>
  12.         <goDrawFocusSelected value="False"/>
  13.         <goRowSizing value="False"/>
  14.         <goColSizing value="False"/>
  15.         <goRowMoving value="False"/>
  16.         <goColMoving value="False"/>
  17.         <goEditing value="False"/>
  18.         <goTabs value="False"/>
  19.         <goRowSelect value="False"/>
  20.         <goAlwaysShowEditor value="False"/>
  21.         <goThumbTracking value="False"/>
  22.         <goColSpanning value="False"/>
  23.         <goRelaxedRowSelect value="False"/>
  24.         <goDblClickAutoSize value="False"/>
  25.         <goSmoothScroll value="True"/>
  26.       </options>
  27.     </design>
  28.     <position topleftcol="1" topleftrow="1" col="1" row="1">
  29.       <selection left="1" top="1" right="1" bottom="1"/>
  30.     </position>
  31.     <content>
  32.       <cells cellcount="10">
  33.         <cell1 column="0" row="0" text="Col1"/>
  34.         <cell2 column="0" row="1" text="1.1"/>
  35.         <cell3 column="0" row="2" text="2.1"/>
  36.         <cell4 column="0" row="3" text="3.1"/>
  37.         <cell5 column="0" row="4" text="4.1"/>
  38.         <cell6 column="1" row="0" text="Col2"/>
  39.         <cell7 column="1" row="1" text="1.2"/>
  40.         <cell8 column="1" row="2" text="2.2"/>
  41.         <cell9 column="1" row="3" text="3.2"/>
  42.         <cell10 column="1" row="4" text="4.2"/>
  43.       </cells>
  44.     </content>
  45.   </grid>
  46. </CONFIG>
and make some cosmetic alterations to your form, your project looks like the screenshot below, which is what is expected?

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Grids and XML save
« Reply #3 on: August 26, 2020, 06:49:09 pm »
@wp you are right, it was a poor description. In my defense I was so discussed at the example not working and I really didn't know how to explain the problem other that it didn't appear to work properly. 

@haroldpc

No go. I get a runtime error Something about a declaration isn't allowed. Line 1 position 7

In your XML you have  <design columncount="2" rowcount="5" fixedcols="1" fixedrows="1" defaultcolwidth="150" defaultRowHeight="22">

FixedCols=1 and fixedRows=1 but in the screen shot it looks like FixedCols=0

I have tried it both ways and get the same error.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018