3

後で使用するためにTStringGridからの情報を格納するために、PascalでレコードタイプTTableDataを作成しました。

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;

ここで何が起こっているのかよくわかりません。ブレークポイントを使用してコードをゆっくりとトラバースすると、データが最初に正しく読み込まれていることがわかります(2番目のforループからtableData [i、j]の上にマウスを置くと、現在の値が表示されます)。ループ自体でShowMessage(...)を実行しようとすると、値が間違って表示されます。

前もって感謝します、

4

2 に答える 2

1

割り当てるときは、セル[Col、Row]をアドレス指定していますが、これは正しいです。制御ループ ( ShowMessage) で [Row, Col] のアドレス指定に切り替えましたが、これは正しくありません。

于 2011-05-02T21:21:38.120 に答える
0

コードに行/列と i/j を混在させています。

これはおそらくあなたが意図していることです:

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;
于 2011-05-02T18:53:45.827 に答える