delphidelphi-7delphi-2010delphi-xe

How to load and save StringGrid content?


First part of the code works OK while the second (commented) does not. It overwrites my A1 file although it should write to A2.

procedure TForm1.AdvGlowButton12Click(Sender: TObject);
var
  i,j: Integer;
  Seznam: TStrings;
  ApplicationPath: string;
begin
  if (cxRadiogroup3.ItemIndex and cxRadiogroup2.ItemIndex) = 0 then begin
    ApplicationPath:= ExtractFileDir(Application.ExeName);
    Seznam:= TStringList.Create;
    try
      for i:=0 to advStringGrid2.ColCount-1 do
        Seznam.AddStrings(advStringGrid2.Cols [i]);
      for i:=0 to advStringGrid2.rowCount-1 do
        Seznam.AddStrings(advStringGrid2.rows [j]);
      Seznam.SaveToFile(ApplicationPath+'\A1.txt');
    finally
      seznam.free;
    end;
  end ;
  //if cxRadiogroup3.ItemIndex = 1 and cxRadiogroup2.ItemIndex = 0 then begin
  //  ApplicationPath:= ExtractFileDir(Application.ExeName);
  //  Seznam:= TStringList.Create;
  //  try
  //    for i:=0 to advStringGrid2.ColCount-1 do
  //      Seznam.AddStrings(advStringGrid2.Cols [i]);
  //    for i:=0 to advStringGrid2.rowCount-1 do
  //      Seznam.AddStrings(advStringGrid2.rows [j]);
  //    Seznam.SaveToFile(ApplicationPath+'\A2.txt');
  //  finally
  //    seznam.free;
  //  end ;
  //end
end;

What am I doing wrong ? Also why is the stringgrid giving listindex out of bounds when I try to load into it contents from an empty text file? If I save empty stringgrid to that file,later ,though it has nothing in the file,it does not complain? Strange...

This is how I load A1 and A2 into the stringgrid.

procedure TForm1.cxRadioGroup2Click(Sender: TObject);
Var
  I,j,k: Integer;
  Seznam: TStrings;
  ApplicationPath: string;
begin
  case cxradioGroup2.ItemIndex of
    0: begin
         if cxradioGroup3.ItemIndex = 0 then begin
           Seznam:= TStringList.Create;
           AdvStringgrid2.ClearAll;
           try
             Seznam.LoadFromFile('A1.txt');
             k:= 0;
             for i:=0 to advStringGrid2.ColCount-1 do
               for j:=0 to advStringGrid2.RowCount-1 do begin
                 advstringGrid2.Cells [i,j]:= Seznam.Strings [k];
                 Inc(k);
               end;
           finally
             seznam.free;
           end;
         end;
         if cxradioGroup3.ItemIndex = 1 then begin
           Seznam:= TStringList.Create;
           AdvStringgrid2.ClearAll;
           try
             Seznam.LoadFromFile('A2.txt');
             k:=0;
             for i:=0 to advStringGrid2.ColCount-1 do
               for j:=0 to advStringGrid2.RowCount-1 do begin
                 advstringGrid2.Cells [i,j]:= Seznam.Strings [k];
                 Inc(k);
               end;
           finally
             seznam.free;
           end;
         end;
       end;
  end;
end;

Solution

  • here is an old tipp from SwissDelphiCenter that could help you

    // Save StringGrid1 to 'c:\temp.txt':
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SaveStringGrid(StringGrid1, 'c:\temp.txt');
    end;
    
    // Load StringGrid1 from 'c:\temp.txt':
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      LoadStringGrid(StringGrid1, 'c:\temp.txt');
    end;
    
    // Save a TStringGrid to a file
    
    procedure SaveStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
    var
      f:    TextFile;
      i, k: Integer;
    begin
      AssignFile(f, FileName);
      Rewrite(f);
      with StringGrid do
      begin
        // Write number of Columns/Rows
        Writeln(f, ColCount);
        Writeln(f, RowCount);
        // loop through cells
        for i := 0 to ColCount - 1 do
          for k := 0 to RowCount - 1 do
            Writeln(F, Cells[i, k]);
      end;
      CloseFile(F);
    end;
    
    // Load a TStringGrid from a file
    
    procedure LoadStringGrid(StringGrid: TStringGrid; const FileName: TFileName);
    var
      f:          TextFile;
      iTmp, i, k: Integer;
      strTemp:    String;
    begin
      AssignFile(f, FileName);
      Reset(f);
      with StringGrid do
      begin
        // Get number of columns
        Readln(f, iTmp);
        ColCount := iTmp;
        // Get number of rows
        Readln(f, iTmp);
        RowCount := iTmp;
        // loop through cells & fill in values
        for i := 0 to ColCount - 1 do
          for k := 0 to RowCount - 1 do
          begin
            Readln(f, strTemp);
            Cells[i, k] := strTemp;
          end;
      end;
      CloseFile(f);
    end;
    

    I'm trying to understand your code and tried him as good as it is possible for me to rewrite. (it's not tested)

    procedure TForm1.AdvGlowButton12Click(Sender: TObject);
    var
      i, j: Integer;
      Seznam: TStrings;
      ApplicationPath: string;
      fileName: string;
      line: string;
    begin
      if (cxRadiogroup2.ItemIndex = 0) then begin
        if (cxRadiogroup3.ItemIndex = 0) then
          fileName:= 'A1.txt'
        else
          fileName:= 'A2.txt'
    
        ApplicationPath:= ExtractFileDir(Application.ExeName);
        Seznam:= TStringList.Create;
        try
          for k:=0 to advStringGrid2.RowCount-1 do begin
            line:= '';
            for i:=0 to advStringGrid2.ColCount-1 do
              line = line + '|' + advStringGrid2.Cells[i, k];
            Seznam.AddStrings(line);
          end;
          Seznam.SaveToFile(ApplicationPath + '\' + fileName);
        finally
          seznam.Free;
        end;
      end;
    end;
    
    procedure TForm1.cxRadioGroup2Click(Sender: TObject);
    var
      splitList: TStringList;
      i, j: Integer;
      Seznam: TStrings;
      ApplicationPath: string;
      fileName: string;
      line: string;
      sepIndex: integer;
    begin
      if (cxRadiogroup2.ItemIndex = 0) then begin
        if (cxRadiogroup3.ItemIndex = 0) then
          fileName:= 'A1.txt'
        else
          fileName:= 'A2.txt'
    
        AdvStringgrid2.ClearAll; // don't know what this does
    
        ApplicationPath:= ExtractFileDir(Application.ExeName);
        Seznam:= TStringList.Create;
        try
          Seznam.LoadFromFile(fileName);
          advstringGrid2.RowCount:= Seznam.Count;
          splitList:= TStringList.Create;
          for i:=0 to Seznam.Count-1 do begin
            line:= Seznam.Strings [i];
            Split('|', line, splitList);
            advStringGrid2.ColCount:= Max(advStringGrid2.ColCount, splitList.Count);
            for k:=0 to splitList.Count-1 do
              advStringGrid2.Cells[i, k]:= splitList[k];
          end; 
        finally
          splitList.Free;
          seznam.Free;
        end;
      end;
    end;
    
    procedure Split (const Delimiter: Char; Input: string; const Strings: TStrings);
    begin
       Assert(Assigned(Strings));
       Strings.Clear;
       Strings.Delimiter:= Delimiter;
       Strings.DelimitedText:= Input;
    end;
    

    hope that helps