• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

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
0
PeterdeB
Asked:
PeterdeB
  • 3
  • 2
1 Solution
 
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
 
ZhaawZSoftware 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
 
PeterdeBAuthor Commented:
Working! Great! :)

Regards Peter
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now