Take the 2-minute tour ×

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.


share|improve this question
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
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
add comment

2 Answers

up vote 4 down vote accepted

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);
  MoreData := (CurrLine < StringGrid1.RowCount);
  if MoreData then
    qrTextLine.Caption := StringGrid1.Cells[0, CurrLine];
    qrTextData.Caption := StringGrid1.Cells[1, CurrLine];
share|improve this answer
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
add comment

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['COL1'] := 'Whatever';
  CS['COL2'] := 'An other thing';

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.

share|improve this answer
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
add comment

Your Answer


