Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filling a String Grid

Status
Not open for further replies.

Creeden

Programmer
May 9, 2002
16
0
0
CA
I have a project that uses Edit boxes for Text Input.
These are saved to a LogRec file XXX.dat
Code:
type  {Log Records}
    TLogRec = record
    Name  : String[15];
    Station  : String[10];
    Freq  : String[10];
    ...
    qslr  : String[5];
    timeoff : String[9];

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    ...
    Edit14: TEdit;
    Edit15: TEdit;

const
  MAXRECS = 2000;

procedure TForm1.SetRecVals;
begin
  with AddrRec do begin
    Name  := Edit1.Text;
    Station  := Edit2.Text;
    Freq  := Edit3.Text;
    Mode  := Edit4.Text;
    qth   := Edit5.text;
    prov  := Edit6.Text;
    power := Edit7.Text;
    rsts  := Edit8.Text;
    rstr  := Edit9.Text;
    date  := Edit10.Text;
    time  := Edit11.Text;
    qsls  := Edit12.Text;
    qslr  := Edit13.Text;
    timeoff := Edit15.Text;
  end;
end;

procedure TForm1.ReadRec;
begin
  Read(AddrFile, AddrRec);
  with AddrRec do begin
    Edit1.Text := Name;
    Edit2.Text := Station ;
    Edit3.Text := Freq ;
    Edit4.Text := Mode ;
    Edit5.text := qth ;
    Edit6.Text := prov   ;
    Edit7.Text := power  ;
    Edit8.Text := rsts;
    Edit9.Text := rstr ;
    Edit10.Text := date;
    Edit11.Text := time;
    Edit12.Text := qsls;
    Edit13.Text := qslr;
    Edit15.Text := timeoff;
    end;
  Seek(AddrFile, FilePos(AddrFile) - 1);
end;
I have Stringgrid1 on my form set up with all the proper columns etc.
How do I load these records into Stringgrid1?
I have looked all over for documentation and can't find what I need. I hope I have given enough information.
Thanks for your help,
Bob
 
hi bob,

I hope you mean something like this.

Read(AddrFile, AddrRec);
with AddrRec do
begin
StringGrid1.Cells[1,StringGrid1.RowCount] := Name;
StringGrid1.Cells[2,StringGrid1.RowCount] := Station;
StringGrid1.Cells[3,StringGrid1.RowCount] := Freq;
StringGrid1.Cells[4,StringGrid1.RowCount] := Mode;
StringGrid1.RowCount := StringGrid1.RowCount +1; // add a row
..
..
end;
Seek(AddrFile, FilePos(AddrFile) - 1);

steph [bigglasses]
 


AssignFile(AddrFile, FileName);
Reset(AddrFile);
for i:= 0 to FileSize(AddrFile)-1 do
begin
Read(AddrFile, AddrRec);
StringGrid1.Cells[0,i+1] := addrRec.Name;
StringGrid1.Cells[1,i+1] := addrRec.Station;
StringGrid1.Cells[2,i+1] := addrRec.Freq;
StringGrid1.Cells[3,i+1] := addrRec.Date;
//etc....
end; Steven van Els
SAvanEls@cq-link.sr
 
Hello Steph.
Thanks for the reply.
What I need is to load all the fields of all records into stringgrid1. There could be 300 records of 13 fields.
Code:
procedure TForm1.btnLoadClick(Sender: TObject);
var
  curPos,
  numRecs,
  I : Integer;
  RecBuf : Array[0..MAXRECS] of TLogRec;
begin
  {First read the fields into the temporary record buffer}
  SetRecVals;
  {Get the current file position}
  curPos := FilePos(AddrFile);
  {Get the total number of records to write}
  numRecs := FileSize(AddrFile);
    I := 0;
    Seek(AddrFile, 0);
    while FilePos(AddrFile) < curPos do begin
      Read(AddrFile, RecBuf[I]);
      Inc(I);
      ...
      ...
      ...    //stringgrid1 in here somewhere!!!!
      ...
    end;
This is part of what I have been messing with. It will have to read the first record. Deposit it in each cell, 13 fields. Then inc a row and do the same with each cell again, but with the next record. Keep repeating until EOF.
You can see the form at on the new projects link. Bottom screen shot
Thanks again,
Bob
 
Hi Steven.
Thanks for the code.
I have a problem with it, an error 103. [mad]
I marked it //HERE in the code below. Don't see why it happens though. Program works fine until I press the Load Button. This is what I ended up with.
Code:
procedure TForm1.LoadClick(Sender: TObject);
var
    TheFileName : String;
    i: Integer;
begin
AssignFile(AddrFile, TheFileName);
     Reset(AddrFile);
     for i:= 0 to FileSize(AddrFile)-1 do  //HERE [mad]
    begin
      Read(AddrFile, AddrRec);
      StringGrid1.Cells[0,i+1] := addrRec.name;
      StringGrid1.Cells[1,i+1] := addrRec.station;
      StringGrid1.Cells[2,i+1] := addrRec.freq;
      StringGrid1.Cells[3,i+1] := addrRec.date;
      StringGrid1.Cells[4,i+1] := addrRec.mode;
      StringGrid1.Cells[5,i+1] := addrRec.qth;
      StringGrid1.Cells[6,i+1] := addrRec.prov;
      StringGrid1.Cells[7,i+1] := addrRec.power;
      StringGrid1.Cells[8,i+1] := addrRec.rsts;
      StringGrid1.Cells[9,i+1] := addrRec.rstr;
      StringGrid1.Cells[10,i+1] := addrRec.time;
      StringGrid1.Cells[11,i+1] := addrRec.timeoff;
      StringGrid1.Cells[12,i+1] := addrRec.qsls;
      StringGrid1.Cells[13,i+1] := addrRec.qslr;
      end;
 end;
You can see the form at...
on the new projects link on the left. Bottom screen shot. The whole Logbook program runs good.
Thanks again,
Bob. [pipe]
 
Ok. It is all working now.
I had the code in the wrong place.
Thank you all for your help.

Is there a way to have the rows in the stringgrid1 added as they are needed, instead of using the RowCount in the Object Inspector?
Thanks again
Bob.
 
The meaning of the error codes for IOresult are:

2 File not found
3 Path not found
5 Access denied
32 Sharing violation

100 End of File
101 Disk Full
102 File variable not assigned
103 File not open
104 File not open for input
105 File not open for output
106 Error in formatted input
107 File already open

You can get the 103 also if the file is read-only, what certainly will happen if it is on a CD.

Regards Steven van Els
SAvanEls@cq-link.sr
 
If I understand you correctly, all you need is something like
StringGrid1.RowCount := FileSize(AddrFile) +1;
This assumes you have a header in row zero.
Or you can add one at a time in the &quot;for i&quot; do loop using
StringGrid1.RowCount := StringGrid1.RowCount +1;
but it is less efficient.
Don't forget to set the initial RowCount value in the object inspector to 2. If you increment RowCount in the do loop you will also need to subtract one from it at the end. This is because you cannot set the grid to have less than one data row, so the loop is always one ahead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top