1

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?

Sam
  • 2,663
  • 10
  • 41
  • 60
  • You can simply wire up the clientdataset to a dbgrid, or am I missing something? – Marco van de Voort Jul 14 '10 at 10:51
  • 1
    Sorry Marco, I'm not using a TDBGrid, I've already got the data into a TStringGrid. I want to generate my report from that StringGrid without requery the DB. – Sam Jul 14 '10 at 22:14

2 Answers2

6

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Thanks Ken, I'll try that. Nice to know I don't necessarily need a CDS. Appreciate your expertise. +1 for you too. – Sam Jul 14 '10 at 22:23
  • I like this answer because it went beyond answering the exact question "CDS from StringGrid, How?" and instead provided an even easier way to acheive the desired outcome (QuickReport from StringGrid). – Sam Jul 14 '10 at 22:27
4

I assume the set of columns is fixed within the StringGrid (and withing the corresponding TClientDataSet). Step-by-step instructions:

  1. Drop a TClientDataSet on the form
  2. Doublic-click on the TClientDataSet, hit the INSERT key on the keyboard to add an new field, add one field for each of your grid's columns. Example: COL1, String, 128 width.
  3. Right-click on the TClientDataSet on the form and hit "Create DataSet"
  4. AT RUNTIME run this kind of code:
  CS.Append;
  CS['COL1'] := 'Whatever';
  CS['COL2'] := 'An other thing';
  CS.Post;

You'll need to do the Append/Post in a loop, looping over each row in the grid. You can assign the COL1, COL2 etc in an other loop, or you can hand-code it.

Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104
  • Thank You. Not bad, probably works (haven't tried it yet), but I wanted to do everything at runtime (no design-time changes). +1 For a good timely answer. – Sam Jul 14 '10 at 22:18