Delphi 将数据导入StringGrid里的函数,在Delphi11.3/12编译通过
两个参数:
AQuery:数据连接源
ASG: StringGrid名称
procedure GetDataToGrid(AQuery:TFDQuery; ASqls:String; ASG:TStringGrid;ALabel:TLabel);
var
iRow,iCol : Integer;
begin
AQuery.Close;
AQuery.SQL.Text := ASqls;
AQuery.Open;
ASG.Row := 1;
ASG.RowCount := AQuery.RecordCount+1;
//显示数据记录
if not AQuery.IsEmpty then
begin
for iRow := 1 to ASG.RowCount-1 do
begin
for iCol := 0 to Min(AQuery.FieldCount-1,ASG.ColCount-1) do
begin
ASG.Cells[iCol,iRow] := AQuery.Fields[iCol].AsString;
end;
AQuery.Next;
//如果已达末尾,则退出
if AQuery.Eof then
begin
break;
end;
end;
end;
ALabel.Caption := '记录数 '+inttostr(AQuery.RecordCount)+' 条';
AQuery.Close;
end;