Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Filling a stringgrid from an array

I need to fill my stringgrid with data stored in a file 'teacherdata.dat' via an array. I have the following code, but when i run the program, only the first column of data is loaded into the stringgrid (the titles of the teachers). Can anyone point me out as to where i'm going wrong?

[code]
teacher = record
title : string[10];
surname : string[10];
initials : string[10];
subject : string[10];
end;

var
Form1: TForm1;
teacherrecord:teacher;
teacherrecord2:teacher2;
tinfo:file of teacher;
tinfo2:file of teacher2;
i,j,col:integer;
teacherinfo: array [0..10] of teacher;
[/code]

[code]
procedure TForm1.FileToArray;
begin
assignfile (tinfo,'teacherdata.dat');
reset (tinfo);
i:=1;
while not eof (tinfo)do
begin
read (tinfo, teacherinfo[i]);
i:=i+1;
end;
closefile(tinfo);
end;
[/code]

[code]
procedure TForm1.ArrayToStringgrid;
begin
for i:=0 to 8 do
form1.stringgrid1.cells[0,i]:=teacherinfo[i].title;
form1.stringgrid1.cells[1,i]:=teacherinfo[i].surname;
form1.stringgrid1.cells[2,i]:=teacherinfo[i].initials;
form1.stringgrid1.cells[3,i]:=teacherinfo[i].subject;
i:=i+1;
end;
[/code]

Comments

  • zibadianzibadian Member Posts: 6,349
    : I need to fill my stringgrid with data stored in a file 'teacherdata.dat' via an array. I have the following code, but when i run the program, only the first column of data is loaded into the stringgrid (the titles of the teachers). Can anyone point me out as to where i'm going wrong?
    :
    : [code]
    : teacher = record
    : title : string[10];
    : surname : string[10];
    : initials : string[10];
    : subject : string[10];
    : end;
    :
    : var
    : Form1: TForm1;
    : teacherrecord:teacher;
    : teacherrecord2:teacher2;
    : tinfo:file of teacher;
    : tinfo2:file of teacher2;
    : i,j,col:integer;
    : teacherinfo: array [0..10] of teacher;
    : [/code]
    :
    : [code]
    : procedure TForm1.FileToArray;
    : begin
    : assignfile (tinfo,'teacherdata.dat');
    : reset (tinfo);
    : i:=1;
    : while not eof (tinfo)do
    : begin
    : read (tinfo, teacherinfo[i]);
    : i:=i+1;
    : end;
    : closefile(tinfo);
    : end;
    : [/code]
    :
    : [code]
    : procedure TForm1.ArrayToStringgrid;
    : begin
    : for i:=0 to 8 do
    : form1.stringgrid1.cells[0,i]:=teacherinfo[i].title;
    : form1.stringgrid1.cells[1,i]:=teacherinfo[i].surname;
    : form1.stringgrid1.cells[2,i]:=teacherinfo[i].initials;
    : form1.stringgrid1.cells[3,i]:=teacherinfo[i].subject;
    : i:=i+1;
    : end;
    : [/code]
    :
    Your ArrayToStringgrid() does actually the following thing:
    [code]
    procedure TForm1.ArrayToStringgrid;
    begin
    for i:=0 to 8 do
    form1.stringgrid1.cells[0,i]:=teacherinfo[i].title;
    // en of for-do loop
    form1.stringgrid1.cells[1,i]:=teacherinfo[i].surname;
    form1.stringgrid1.cells[2,i]:=teacherinfo[i].initials;
    form1.stringgrid1.cells[3,i]:=teacherinfo[i].subject;
    i:=i+1;
    end;
    [/code]
    You need to enclose all the assignments with an begin-end pair to make it a block which is run 9 times. Also the for-do loop already increases i for each pass. Thirdly, although this is not an error as such, Delphi automatically includes the entire method in a with-do assignment, so you don't need to keep repeating "form1" for each line.
    [code]
    procedure TForm1.ArrayToStringgrid;
    begin
    for i:=0 to 8 do [b]begin[/b]
    stringgrid1.cells[0,i]:=teacherinfo[i].title;
    stringgrid1.cells[1,i]:=teacherinfo[i].surname;
    stringgrid1.cells[2,i]:=teacherinfo[i].initials;
    stringgrid1.cells[3,i]:=teacherinfo[i].subject;
    [b]end;[/b]
    end;
    [/code]

  • Justin BibJustin Bib USAMember Posts: 0

    < http://forcoder.org /> free video tutorials and ebooks about \ Go Objective-C Assembly Visual Basic .NET MATLAB PHP C# Swift C JavaScript Java Scratch C++ Perl Ruby Visual Basic Python Delphi R PL/SQL Apex Awk Julia Rust Prolog Transact-SQL Erlang Dart Scala COBOL Alice Ada FoxPro D Hack Lua F# VBScript Lisp ML LabVIEW Scheme ABAP SAS Kotlin Fortran Logo Bash Clojure Crystal \ _______

Sign In or Register to comment.