How to create a QuickReport from the contents of a TStringGrid

妖精的绣舞 提交于 2019-12-08 06:05:39

问题


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?


回答1:


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;



回答2:


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.



来源:https://stackoverflow.com/questions/3244249/how-to-create-a-quickreport-from-the-contents-of-a-tstringgrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!