whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

42 Visitors Online


 
...Save/Load a TStringGrid to/from a file?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (108):  
     


// 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;


// 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;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners