Einzelnen Beitrag anzeigen

toyoman

Registriert seit: 2. Jun 2003
323 Beiträge
 
Delphi 2010 Enterprise
 
#2

Re: StringGrid speichern/Laden

  Alt 20. Dez 2007, 09:49
Eine StringGrid in eine Datei speichern kann man z.b. so: Auszug aus einem Projekt

Delphi-Quellcode:
procedure tabellespeichern;
type
 hdatei=record sp01,sp02,sp03,sp04,sp05,sp06,sp07,sp08,sp09,sp10,sp11,
               sp12,sp13,sp14,sp15,sp16,sp17,sp18,sp19,sp20,sp21,sp22,
               sp23,sp24,sp25,sp26,sp27,sp28,sp29,sp30,sp31:string[4];
               sp32 :string[255]; {ex30}
        end;
var
 f :file of hdatei;
 hdateiline :hdatei;
 x :integer;

begin
 datei:=dateiname;
 assignfile(f,datei);
 {$I-}
 rewrite(f);
 {$I+}
 if ioresult=0 then
  begin
   for x:= 1 to 24 do
    begin
     {felder 1-31 in hdateiline einlesen-/-bemerkungen 24x abspeichern----}
     hdateiline.sp01:=form51.stringgrid1.cells[1,x];
     hdateiline.sp02:=form51.stringgrid1.cells[2,x];
     hdateiline.sp03:=form51.stringgrid1.cells[3,x];
     hdateiline.sp04:=form51.stringgrid1.cells[4,x];
     hdateiline.sp05:=form51.stringgrid1.cells[5,x];
     hdateiline.sp06:=form51.stringgrid1.cells[6,x];
     hdateiline.sp07:=form51.stringgrid1.cells[7,x];
     hdateiline.sp08:=form51.stringgrid1.cells[8,x];
     hdateiline.sp09:=form51.stringgrid1.cells[9,x];
     hdateiline.sp10:=form51.stringgrid1.cells[10,x];
     hdateiline.sp11:=form51.stringgrid1.cells[11,x];
     hdateiline.sp12:=form51.stringgrid1.cells[12,x];
     hdateiline.sp13:=form51.stringgrid1.cells[13,x];
     hdateiline.sp14:=form51.stringgrid1.cells[14,x];
     hdateiline.sp15:=form51.stringgrid1.cells[15,x];
     hdateiline.sp16:=form51.stringgrid1.cells[16,x];
     hdateiline.sp17:=form51.stringgrid1.cells[17,x];
     hdateiline.sp18:=form51.stringgrid1.cells[18,x];
     hdateiline.sp19:=form51.stringgrid1.cells[19,x];
     hdateiline.sp20:=form51.stringgrid1.cells[20,x];
     hdateiline.sp21:=form51.stringgrid1.cells[21,x];
     hdateiline.sp22:=form51.stringgrid1.cells[22,x];
     hdateiline.sp23:=form51.stringgrid1.cells[23,x];
     hdateiline.sp24:=form51.stringgrid1.cells[24,x];
     hdateiline.sp25:=form51.stringgrid1.cells[25,x];
     hdateiline.sp26:=form51.stringgrid1.cells[26,x];
     hdateiline.sp27:=form51.stringgrid1.cells[27,x];
     hdateiline.sp28:=form51.stringgrid1.cells[28,x];
     hdateiline.sp29:=form51.stringgrid1.cells[29,x];
     hdateiline.sp30:=form51.stringgrid1.cells[30,x];
     hdateiline.sp31:=form51.stringgrid1.cells[31,x];
     if x<=12 then
      begin
       hdateiline.sp32:=bemerkungen[x];
      end else
      begin
       hdateiline.sp32:='?';
     end;
     {--------------------------------------------------------------------}
     seek(f,filesize(f));write(f,hdateiline);
   end;
   closefile(f);
  end else
  begin
   showmessage(datei+' Pfad nicht gefunden');
 end;
end;

Laden könnte man so machen:

Delphi-Quellcode:
procedure tabelleladen;
type
 hdatei=record sp01,sp02,sp03,sp04,sp05,sp06,sp07,sp08,sp09,sp10,sp11,
               sp12,sp13,sp14,sp15,sp16,sp17,sp18,sp19,sp20,sp21,sp22,
               sp23,sp24,sp25,sp26,sp27,sp28,sp29,sp30,sp31:string[4];
               sp32 :string[255]; {ex30}
        end;
var
 f :file of hdatei;
 hdateiline :hdatei;
 x,y :integer;

begin
 nullsetzen;
 for x:= 1 to 31 do
  begin
   for y:= 1 to 24 do
    begin
     form51.stringgrid1.cells[x,y]:='';
   end;
 end;
 datei:=dateiname
 assignfile(f,datei);
 {$I-}
 reset(f);
 {$I+}
 if ioresult=0 then
  begin
   for x:=1 to 24 do
    begin
     seek(f,x-1);read(f,hdateiline);
     {felder 1-31 in hdateiline einlesen-/-bemerkungen 24x laden ----}
     form51.stringgrid1.cells[1,x]:=hdateiline.sp01;
     form51.stringgrid1.cells[2,x]:=hdateiline.sp02;
     form51.stringgrid1.cells[3,x]:=hdateiline.sp03;
     form51.stringgrid1.cells[4,x]:=hdateiline.sp04;
     form51.stringgrid1.cells[5,x]:=hdateiline.sp05;
     form51.stringgrid1.cells[6,x]:=hdateiline.sp06;
     form51.stringgrid1.cells[7,x]:=hdateiline.sp07;
     form51.stringgrid1.cells[8,x]:=hdateiline.sp08;
     form51.stringgrid1.cells[9,x]:=hdateiline.sp09;
     form51.stringgrid1.cells[10,x]:=hdateiline.sp10;
     form51.stringgrid1.cells[11,x]:=hdateiline.sp11;
     form51.stringgrid1.cells[12,x]:=hdateiline.sp12;
     form51.stringgrid1.cells[13,x]:=hdateiline.sp13;
     form51.stringgrid1.cells[14,x]:=hdateiline.sp14;
     form51.stringgrid1.cells[15,x]:=hdateiline.sp15;
     form51.stringgrid1.cells[16,x]:=hdateiline.sp16;
     form51.stringgrid1.cells[17,x]:=hdateiline.sp17;
     form51.stringgrid1.cells[18,x]:=hdateiline.sp18;
     form51.stringgrid1.cells[19,x]:=hdateiline.sp19;
     form51.stringgrid1.cells[20,x]:=hdateiline.sp20;
     form51.stringgrid1.cells[21,x]:=hdateiline.sp21;
     form51.stringgrid1.cells[22,x]:=hdateiline.sp22;
     form51.stringgrid1.cells[23,x]:=hdateiline.sp23;
     form51.stringgrid1.cells[24,x]:=hdateiline.sp24;
     form51.stringgrid1.cells[25,x]:=hdateiline.sp25;
     form51.stringgrid1.cells[26,x]:=hdateiline.sp26;
     form51.stringgrid1.cells[27,x]:=hdateiline.sp27;
     form51.stringgrid1.cells[28,x]:=hdateiline.sp28;
     form51.stringgrid1.cells[29,x]:=hdateiline.sp29;
     form51.stringgrid1.cells[30,x]:=hdateiline.sp30;
     form51.stringgrid1.cells[31,x]:=hdateiline.sp31;
     if x<=12 then
      begin
       bemerkungen[x]:=hdateiline.sp32;
     end;
     {--------------------------------------------------------------------}
   end;
   closefile(f);
   form51.edit1.text:=bemerkungen[1];form51.edit2.text:=bemerkungen[2];
   form51.edit3.text:=bemerkungen[3];form51.edit4.text:=bemerkungen[4];
   form51.edit5.text:=bemerkungen[5];form51.edit6.text:=bemerkungen[6];
   form51.edit24.text:=bemerkungen[7];form51.edit25.text:=bemerkungen[8];
   form51.edit26.text:=bemerkungen[9];form51.edit27.text:=bemerkungen[10];
   form51.edit28.text:=bemerkungen[11];form51.edit29.text:=bemerkungen[12];
  end else
  begin
 end;
end;
Im Beispiel wird ein Kalender geladen /gespeichert. Pro Tag gibt es zwei Felder.

Natürlich können die zwei Prozeduren nicht 1:1 verwendet werden. Es sind viele zusätzliche Sachen drin die du nicht brauchst. Aber evtl. zeigt dir das Beispiel wie du es selber lösen kannst...
  Mit Zitat antworten Zitat