StringGridの使用方法(2)

 

AS/400のデータを表示
セルを右・左・中央に寄せる
1行おきにセルの色を変える
セルの幅を文字列に合わせる
AS/400のデータを表示
2/3
2/3

ボタンを押下したときにデータを読み込んで表示します。行と列の数や見出しをファイルから動的に設定します。


procedure TfrmShain.Button1Click(Sender: TObject);
var
  i, iMax, j, jMax: Integer;
begin
  //ファイルOpen
  Table1.Open;
  //行と列のMAX値をファイルの件数とフィールド数より設定
  iMax := StringGrid1.FixedRows + Table1.RecordCount - 1;
  jMax := StringGrid1.FixedCols + Table1.FieldCount - 1;
  StringGrid1.RowCount := iMax + 1;
  StringGrid1.ColCount := jMax + 1;

  //ファイルよりStringGridに値を設定
  //見出しを先頭行にセット
  for j := StringGrid1.FixedCols to jMax do
  begin
    StringGrid1.Cells[j, 0] := Table1.Fields[j - StringGrid1.FixedCols].FieldName;
  end;
  //データ
  for i := StringGrid1.FixedRows to iMax do
  begin
    for j := StringGrid1.FixedCols to jMax do
    begin
      StringGrid1.Cells[j, i] := Table1.Fields[j - StringGrid1.FixedCols].AsString;
    end;
    Table1.Next;
  end;

  //ファイルClose
  Table1.Close;
end;


2/3