要将stringGrid中的内容保存起来,以便下次装入stringGrid时可将原来的值载入,该如何做?

解决方案 »

  1.   

    要是保存在数据库的话,将每一行作为一条记录,每一列作为一个字段
    要是保存到文件的话:以下是保存到文本文件及读取
    procedure TForm1.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 TForm1.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;
      

  2.   

    你可以下载这个控件tms grid,直接有保存、载入。www.51delphi.com找