我已经在Pascal中创建了一个记录类型TTableData,用于存储从TStringGrid获取的信息以供后续使用:
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;
但是,每当我试图通过遍历TStringGrid并从单元格中获取值来初始化这些对象时,这些值就会变成('','',0)(除了一些单元格以外,它们的值似乎还可以)。
以下是我读取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;
我不太确定这里发生了什么。当我使用断点并缓慢遍历代码时,我可以看到数据最初被正确读入(通过将鼠标悬停在第二个for循环中的tableData [i,j]上以查看其当前值),但是当我尝试在循环本身中使用ShowMessage(...)时,该值就变得错误了。
提前感谢。
tableData[Col,Row]
,而在第二个循环中,您使用tableData[Row,Col]
。 - Jørn E. Angeltveit