delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (5)


Printing a DBGrid Content's with QuickReportGo to Rafael Ribas Aguiló's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Creating a Quick Report layout in runtime to print DBGrid's contents
Product:
Delphi 3.x (or higher)
Category:
Reporting
Skill Level:
Scoring:
Last Update:
11/23/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet Print DBGrid QuickReport
Times Scored:
20
Visits:
17718
Uploader: Rafael Ribas Aguiló
Company: SEF-RJ
Reference: http://www.facilities.com.br
Component Download: http://www.facilities.com.br
 
Question/Problem/Abstract:
Almost always your the user of your application needs to print the records he/she is seeing by scrolling a DBGrid. And always he/she asks you to make a new report that display columns the way he/she thinks.
Now with this code you can provide a more flexible way to print a dataset's record set. Give to you user a DBGrid and let him/her play with it, make even methods to change the font type and colors, and then put this piece of code in a form that has a TQuickRep on it.
Answer:



First make a new form, name it as TGridReport and drop a TQuickRep on it.
Rename you QuickRep to GridRep. Then make a Preview that receives a DBGrid as parameter, just like this:



procedure TGridReport.Preview(Grid: TDBGrid);
var
  i, CurrentLeft, CurrentTop : integer;
  BMark: TBookmark;
begin
  GridRep.Dataset:=Grid.DataSource.DataSet;

  if not GridRep.Bands.HasColumnHeader then
    GridRep.Bands.HasColumnHeader:=true;

  if not GridRep.Bands.HasDetail then
    GridRep.Bands.HasDetail:=true;

  GridRep.Bands.ColumnHeaderBand.Height:=Abs(Grid.TitleFont.Height) + 10;
  GridRep.Bands.DetailBand.Height:=Abs(Grid.Font.Height) + 10;
  CurrentLeft := 12;
  CurrentTop := 6;

  {Record where the user stopped in the DBGrid}
  BMark:=Grid.DataSource.DataSet.GetBookmark;
  {Don't let the grid flicker while the report is running}
  Grid.DataSource.DataSet.DisableControls;
  try
    for i:=0 to Grid.FieldCount - 1 do
    begin
      if (CurrentLeft + Canvas.TextWidth(Grid.Columns[i].Title.Caption)) >
        (GridRep.Bands.ColumnHeaderBand.Width) then
      begin
        CurrentLeft := 12;
        CurrentTop := CurrentTop + Canvas.TextHeight('A') + 6;
        GridRep.Bands.ColumnHeaderBand.Height := GridRep.Bands.ColumnHeaderBand.Height +
          (Canvas.TextHeight('A') + 10);
        GridRep.Bands.DetailBand.Height := GridRep.Bands.DetailBand.Height +
          (Canvas.TextHeight('A') + 10);
      end;
      {Create Header with QRLabels}
      with TQRLabel.Create(GridRep.Bands.ColumnHeaderBand) do
      begin
        Parent := GridRep.Bands.ColumnHeaderBand;
        Color := GridRep.Bands.ColumnHeaderBand.Color;
        Left := CurrentLeft;
        Top := CurrentTop;
        Caption:=Grid.Columns[i].Title.Caption;
      end;
      {Create Detail with QRDBText}
      with TQRDbText.Create(GridRep.Bands.DetailBand) do
      begin
        Parent := GridRep.Bands.DetailBand;
        Color := GridRep.Bands.DetailBand.Color;
        Left := CurrentLeft;
        Top := CurrentTop;
        Alignment:=Grid.Columns[i].Alignment;
        AutoSize:=false;
        AutoStretch:=true;
        Width:=Grid.Columns[i].Width;
        Dataset:=GridRep.Dataset;
        DataField:=Grid.Fields[i].FieldName;
        CurrentLeft:=CurrentLeft + (Grid.Columns[i].Width) + 15;
      end;
    end;

    lblPage.Left := bdTitle.Width - lblPage.Width - 10;
    lblDate.Left := bdTitle.Width - lblDate.Width - 10;

    {After all, call the QuickRep preview method}
    GridRep.PreviewModal; {or Preview if you prefer}

  finally
    with Grid.DataSource.DataSet do
    begin
      GotoBookmark(BMark);
      FreeBookmark(BMark);
      EnableControls;
    end;
  end;
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Doesn't work when not fit to page:
    Marc Convents (Jun 15 2001 3:26AM)

{ another solution, no problems when not fitting on page }

unit Unit2;

interface
uses QuickRpt,  qrextra, qrprntr, qrctrls,db,dbgrids,classes;

procedure MakeReport(AGrid: TDBGrid; ATitle: string; JustPreview : boolean= true);

implementation

procedure MakeReport(AGrid: TDBGrid; ATitle: string; JustPreview : boolean= true);
var
I : integer;   AD: TDataset;
AR: TCustomQuickRep;    Camps: TStringList;
begin
  AD := AGrid.DataSource.DataSet;
  if not AD.active then
     AD.Open;
  Camps := TStringList.Create;

  try
{ Just print the visible columns of AGrid }
    with AGrid, Columns  do
      for i := 0 to Count - 1 do
         if Columns[i].Visible then
            Camps.Add(Columns[i].FieldName);

    AR := nil;
    QRCreateList(AR, Nil, AGrid.DataSource.DataSet, ATitle, Camps);

{ Report column's width must be equal to AGrid column's width, don't you think?}
    with AR do
      for i := 0 to ComponentCount - 1 do
      begin
        if (Components[i] is TQRDBText)then
          TQRDBText(Components[i]).Width := AGrid.Columns[AD.FieldByName(TQRDBtext(Components[i]).DataField).Index].Width
        else if (Components[i] is TQRLabel) then
          if AD.FindField(TQRLabel(Components[i]).Caption)<>nil then
             TQRLabel(Components[i]).Width := AGrid.Columns[AD.FieldByName(TQRLabel(Components[i]).Caption).Index].Width;
      end;

//preview or print
    if JustPreview then
       AR.Preview
    else
    AR.Print;
  finally
    AR.Free;
    camps.Free;
  end;

end;

end.
Respond

RE: Doesn't work when not fit to page:
Rafael Ribas Aguiló (Jun 15 2001 2:54PM)

Hi,
The width was set, but you don't have to worry about it, the AutoStretch property of the TQRLabel takes care of it, if the text don't fit it breaks the line automatically. See the piece of code extracted the original:

AutoStretch:=true;
Width:=Grid.Columns[i].Width;

Respond

Cant get it to Work :(
martin hayes (Nov 18 2004 11:37AM)

Hi

Could you please send me a sample of this working ?? I am having big difficulty !

I am working on Delphi 4 - could this be the problem ??

Thanks Lots
    Martin
Respond

RE: Cant get it to Work :(
martin hayes (Nov 18 2004 11:41AM)

My email is:  martin_john_hayes@hotmail.com

Thanks again !!
           Martin
Respond

i couldn't did it
nuri (May 14 2005 10:57PM)

could you send me an example where you print dbgrid contents. thanks
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
E. DSpirito
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)