Search code examples
delphifastreportdelphi-xe8

Printing listview items using fastreport in Delphi


I am looking for a simple example or a reference to print items from a listview using fastreport. For example: I have a listview that has three columns: id, name and marks. The listview has n number items each containing id, name and marks of individual students. I need to print the whole listview using fastreport in a tabular format. I googled but couldn't find an exact sample for my scope. Similarly, I couldn't find any documentation regarding this in fastreport demo. I am using Delphi XE8 with default installed fastreport version.


Solution

  • For demonstration purposes.

    Place on the form the following components:

    • TButton;
    • TListView;
    • frxReport;
    • frxUserDataSet;

    Double click on frxReport. In the Fastreport designer do

    insert ReportTitle, MasterData and PageFooter bands.

    Press Report => Data menu items. Check frxUserDataSet check box and press OK button.

    Assign MasterData band with frxUserDataSet as double click on MasterData band and select frxUserDataSet, then press 'OK' button.

    In MasterData band insert Text object (Memo). In Memo write [element].

    Designer should look like this :

    enter image description here

    Now we can write some code :

    procedure TForm8.FormCreate(Sender: TObject);
    var
      I: Integer;
      Li : TlistItem;
    begin
      // Just to fill some data in ListView 
      for I := 1 to 10 do
       begin
        Li := ListView1.Items.Add;
        LI.Caption := 'Col ' + IntToStr(i);
       end;
    
    end;
    

    In frxReport1 OnGetValue event write for example :

    procedure TForm8.frxReport1GetValue(const VarName: string; var Value: Variant);
    begin
      if CompareText(VarName, 'element') = 0 then
        Value := ListView1.Items[frxUserDataSet1.RecNo].Caption;
    end;
    

    And now print data

    procedure TForm8.Button1Click(Sender: TObject);
    begin
      frxUserDataSet1.RangeEnd := reCount;
      frxUserDataSet1.RangeEndCount := ListView1.Items.Count;
      frxReport1.ShowReport();
    end;
    

    The result after Button1 is pressed:

    enter image description here

    Note : In this answer is used part of FastReport PrintStringList demo.