Re: How to transfer a text file into DBGrid?


[ DelphiLand Discussion Forum ]

Posted by webmaster Guido on April 05, 2003 at 13:20:35:

In Reply to: How to transfer a text file into DBGrid? posted by sam sgling07 on April 04, 19103 at 15:23:37:

: Hi,
: can someone help me on this. HOw to transfer the content of a text file(there are many rows) and put it on DBGrid? (Only one column is needed in DBGrid).

: Thanks for help.

---------

The easiest way to do this:
1. Create a temporary stringlist
2. Read the textfile into the stringlist
3. Put every item of the stringlist in a new record of the table
4. Destroy the temporary stringlist

Let's assume that you use a TTable component Table1 to receive the textlines, and Table1 has a field called Textline that's large enough to hold the longest line of text.
The structure of a procedure that reads in the textfile (parameter TxtFile contains the full path of the textfile):

procedure TForm1.AppendTxtToTable(TxtFile: string);
var
  i: integer;
  SL: TStringList;
begin
  SL := TStringList.Create;
  SL.LoadFromFile(TxtFile);
  for i := 0 to SL.Count - 1 do begin
    Table1.Append;
    Table1TextLine.Value := SL[i];
    Table1.Post;
  end;
  SL.Free;
end;

Related Articles and Replies:


[ Post Followup ] [ DelphiLand Discussion Forum ]