procedure TForm7.FormActivate(Sender: TObject); var i: integer; t,r: tstringlist; begin StatusBar1.Panels[0].Text:=Form1.CheckListBox1.Items.ValueFromIndex[Form1.CheckListBox1.ItemIndex]; try t:=tstringlist.Create; r:=tstringlist.Create; begin t.LoadFromFile('files/Res/Prepod/1.'+inttostr(Form1.CheckListbox1.ItemIndex)+'spisok.txt'); for i:=1 to stringgrid1.RowCount-1 do stringgrid1.Rows[i].Clear; stringgrid1.ColCount:=1; stringgrid1.RowCount:=t.Count; for i:=1 to t.Count-1 do begin r.DelimitedText:=t[i]; if r.Count>stringgrid1.ColCount then stringgrid1.ColCount:=r.Count; stringgrid1.Rows[i].Assign(r); end; end; except on E:exception do showmessage('Файла не существует'); end; end; 

There is a code that unloads data from a file inside the root folder and outputs it to a StringGrid . How to make the inverse function?

Here is the input information: outgoing information

And it turns out outgoing if I unload already from the table. outgoing

How to make it save as input?

  • Go through Cells [i] [j] and write. - cpp_user
  • Correct indents in the code. They are misleading. - Kromster

1 answer 1

As I understand it - the one column, it simplifies the task.
//stringgrid1.ColCount:=1;
If there were more columns, then it would be possible to add a loop on them and slightly change the file format. Of course, you can fit under any, but I understand that now just one line. String = value.

 procedure TForm7.SaveStingGrid2File(FileName : string); var i: integer; StringListt: TStringList; begin StringList := TStringList.Create; try for i := 1 to stringgrid1.RowCount-1 do StringList.Add(stringgrid1.Cells[0, i]); StringList.SaveToFile(FileName); finally StringList.free; end; end; 

For several columns, you can print the number of columns in the first line, and then save along with the cycle by columns

 procedure TForm7.SaveStingGrid2File(FileName : string); var i, j: integer; StringList: TStringList; begin StringList := TStringList.Create; try StringList.Add(IntToStr(stringgrid1.ColCount)); for i := 1 to stringgrid1.RowCount-1 do for j := 1 to stringgrid1.ColCount-1 do StringList.Add(stringgrid1.Cells[j, i]); StringList.SaveToFile(FileName); finally StringList.free; end; end; 

By the way, a recommendation from me personally (don't consider it impudent)
1) It is better to call variables and components meaningfully.
Form7, StringGrid1 - when there are tens and hundreds, it is easy to get confused in your code, even the next day. If one form, then not so relevant.
2) Try except it is better to put directly in front of the critical code with a potential error, so it is more readable
try
loadfromfile
except
end
3) Functions better to do more versatile.
Not a procedure TForm7.SaveStingGrid2File (FileName: string);
and the procedure SaveStingGrid2File (StringGrid: TStringGrid; FileName: string);
This will help avoid duplicate code.

  • one
    Do not teach people to write the wrong code. An empty except end construction (without exception handling) is a very harmful construction. - kot-da-vinci
  • You misunderstood the code. ColCount := 1 is just a dump of old cells (why - let's leave it on the conscience of the author). And then it comes with stringgrid1.ColCount:=r.Count; - kami