views:

150

answers:

2

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?

+3  A: 

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
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
+2  A: 

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
Thanks Ken, I'll try that. Nice to know I don't necessarily need a CDS. Appreciate your expertise. +1 for you too.
Sam
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