How to fill a TStringgrid with a textfile?

Hi Folks!

I have a TStringgrid and want to fill it with the contents of a textfile. Question is HOW??

Regards,

Peter
PeterdeBAsked:
Who is Participating?
 
ZhaawZConnect With a Mentor Software DeveloperCommented:
If I understood what you need, this should do the trick:

var
  f : file;
  arr : array of char;
  n : cardinal;
begin
AssignFile(f, 'd:\test.txt');
FileMode := fmOpenRead;
reset(f, 1);
SetLength(arr, filesize(f));
BlockRead(f, arr[0], length(arr));
CloseFile(f);
for n := 0 to high(arr) do StringGrid1.Cells[n mod 20, n div 20] := arr[n];
end;
0
 
ZhaawZSoftware DeveloperCommented:
How do you want to fill? What are the dellimiters in your text file?
Is there only one row/column or there are multiple columns?
0
 
PeterdeBAuthor Commented:
Hi ZhaawZ!

20 Rows and 20 Columns > I have 400 characters ranging from A to Z which I want to dump into the Stringgrid. Delimiter doesn't matter to me, I have no preference for some delimiter as long as I can get the text into the Stringrid

Peter
0
 
PeterdeBAuthor Commented:
Randomly filled if possible btw

Peter
0
 
PeterdeBAuthor Commented:
Working! Great! :)

Regards Peter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.