Reputation: 39
I've created a record type TTableData in Pascal for storing information from a TStringGrid for later use:
TTableData = record
header: String[25]; //the header of the column (row 0)
value : String[25]; //the string value of the data
number: Integer; //the y-pos of the data in the table
end;
But whenever I try to initialize these objects by traversing through the TStringGrid and obtaining values from cells the values become ('','',0) (With the exception of a few cells whhich somehow turn out alright).
Here's my procedure for reading in the data from the TStringGrid:
procedure TfrmImportData.butDoneClick(Sender: TObject);
begin
Halt;
end;
{ initialize records which are responsible
for storing all information in the table itself }
procedure TfrmImportData.initTableDataObjects();
var
i, j: Integer;
begin
SetLength(tableData, StringGrid1.ColCount, StringGrid1.RowCount);
for j:= 0 to StringGrid1.RowCount-1 do begin
for i:= 0 to StringGrid1.ColCount-1 do begin
with tableData[i,j] do begin
header := StringGrid1.Cells[i,0];
value := StringGrid1.Cells[i,j];
number := i;
end;
end;
end;
for i:= 0 to StringGrid1.RowCount - 1 do begin
for j:=0 to StringGrid1.ColCount - 1 do begin
ShowMessage(tableData[i,j].header+': '+tableData[i,j].value);
end;
end;
end;
I'm not really sure whats happening here. When I use breakpoints and traverse through the code slowly, I can see that the data is initially being read in correctly (through holding the mouse over the tableData[i,j] from the second for-loop to see its current value) but when I try to ShowMessage(...) in the loop itself the value comes out wrong.
Thanks in advance,
Upvotes: 3
Views: 3505
Reputation: 1189
When assigning you are addressing the cells[Col, Row], which is correct. In your control loop (ShowMessage
) you have switched to addressing [Row, Col], which is incorrect.
Upvotes: 1
Reputation: 24086
You're mixing row/col and i/j in your code.
This is probably what you intend to do:
procedure TfrmImportData.initTableDataObjects();
var
i, j: Integer;
begin
SetLength(tableData, StringGrid1.RowCount, StringGrid1.ColCount);
for i:= 0 to StringGrid1.RowCount-1 do begin
for j:= 0 to StringGrid1.ColCount-1 do begin
with tableData[i,j] do begin
header := StringGrid1.Cells[i,0];
value := StringGrid1.Cells[i,j];
number := i;
end;
end;
end;
for i:= 0 to StringGrid1.RowCount - 1 do begin
for j:=0 to StringGrid1.ColCount - 1 do begin
ShowMessage(tableData[i,j].header+': '+tableData[i,j].value);
end;
end;
end;
Upvotes: 0