データベースに登録されているデータをGridに表示させたいのですが、
表示の際には、通し番号を付加させたいのです。
なので、TStringGridを使うんだと思うんですが、書き方が全くわかりません。
書き方を教えて下さい。
データーベースはTQueryを使った場合
var
  I,
  Counter:Integer;
begin
  with StringGrid1,Query1 do
  begin
    First;
    Counter := 1;
    RowCount := RecordCount + 1;
    ColCount := Fields.count + 1;
    for I := 1 to Fields.count do
      Cells[I,0] := Fields[I].DisplayLabel;
    while Not Eof do
    begin
      Cells[0,Counter] := IntToStr(Counter);
      for I := 1 to Fields.count do
        Cells[I,0] := Fields[I].asString;
      Next;
      Inc(Counter);
    end;
  end;
end;
目くら打ちなので、スペルが間違っているかもしれません。
色々方法は有りますが・・・・
DataSetを使った場合
//例えばデータの処理手続き内で
(略)
ADODataSet1.Open;
if ADODataSet1.RecordCount > 0 then
begin
 ADODataSet1.FindFirst;
 for i := 0 to ADODataSet1.RecordCount -1 do
 begin
   StringGrid1.Cells[1,i+1] := IntToStr(i+ 1)//通し番号
   StringGrid1.Cells[2,i+2] := ADODataSet1.FieldByName('氏名').AsString;
    ・
    ・
    ・
   StringGrid1.RowCount := StringGrid1.RowCount + 1;
   ADODataSet1.Next;
 end;
 StringGrid1.RowCount := StringGrid1.RowCount -1;
end;
ADODataSet1.Close;
(略)
てな具合で、出来ます。
文字列の配置(右寄せ、センタリングなど)はOnDrawCellイベントで処理してください。
ネットで検索すると色々出てきます。
ありがとうございます〜。
特にSyakeさん。
文字列の配置まで書いていただいて本当に感謝しています。
| ツイート |   |