Search code examples
delphiquickreports

How to create a QuickReport from the contents of a TStringGrid


I'm using Delphi 7 and QuickReports on Windows 7. Normally QuickReports require a DataSet generated by a query, but I want to make a report from the contents of a StringGrid as though the StringGrid is a representation of the results of a query.

How?


Solution

  • Use the QuickReport.OnNeedData event handler. It passes a var parameter called MoreData (a boolean); setting it to True means it gets called again. Leave the QuickReport.DataSource property blank, and use plain TQRText controls rather than TQRDBText.

    // CurrLine is an Integer. In your case, it can represent a row in the StringGrid.
    procedure TPrintLogForm.QuickRep1NeedData(Sender: TObject;
                          var MoreData: Boolean);
    begin
      MoreData := (CurrLine < StringGrid1.RowCount);
      if MoreData then
      begin
        qrTextLine.Caption := StringGrid1.Cells[0, CurrLine];
        qrTextData.Caption := StringGrid1.Cells[1, CurrLine];
        Inc(CurrLine);
      end;
    end;