3

How can I print a DBGrid without installing or downloading components?

OR

How can I get a DBGrid's data into a RichEdit so that I can print it from there?

bluish
  • 26,356
  • 27
  • 122
  • 180
Arno
  • 31
  • 1
  • 2

3 Answers3

2

Data aware controls get their data from DataSource property, use that. You have to manually traverse it though, no instant way possible (without third party libraries / components).

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
1

You will need to be able to work out an appropriate print width for each field, something along these lines:

function PrintFieldWidth(Field: TField): Integer;
var
  CharWidth: Integer;  // an average character width
  TitleWidth: Integer; // the width of the field title
  FieldWidth: Integer; // the width of the field content
begin
  CharWidth := Printer.Canvas.TextWidth('0');                
  TitleWidth := Printer.Canvas.TextWidth(Field.DisplayName);
  FieldWidth := Field.DisplayWidth*CharWidth;               
  if TitleWidth > FieldWidth then
    Result := TitleWidth+CharWidth
  else
    Result := FieldWidth+CharWidth;
end;

Then loop through all the records, and loop through each field to print.

procedure PrintText(S: String; X, Y, W, H: Integer);
begin
  Printer.Canvas.TextRect(Rect(X,Y,X+W,Y+H),S);
end;

procedure PrintHeader(DataSet: TDataSet; X, Y, H: Integer);
var
  I: Integer; // record loop
  W: Integer; // field width
begin
  for I := 0 to DataSet.FieldCount-1 do
  begin
    if DataSet.Fields[I].Visible then
    begin
      W := PrintFieldWidth(DataSet.Fields[I]);
      PrintText(DataSet.Fields[I].FieldName, X, Y, W, H);
      X := X + W;
    end;
  end;
end;

procedure PrintRecord(DataSet: TDataSet; X, Y, H: Integer);
var
  I: Integer; // record loop
  W: Integer; // field width
begin
  for I := 0 to DataSet.FieldCount-1 do
  begin
    if DataSet.Fields[I].Visible then
    begin
      W := PrintFieldWidth(DataSet.Fields[I]);
      PrintText(DataSet.Fields[I].AsString, X, Y, W, H);
      X := X + W;
    end;
  end;
end;

procedure PrintDataSet(DataSet: TDataSet; X, Y: Integer);
var
  OldPos: TBookmark;
  H: Integer; // line height
begin
  if DataSet <> nil then
  begin
    H := Printer.Canvas.TextHeight('0');
    SaveAfterScroll := DataSet.AfterScroll;
    DataSet.AfterScroll := nil;
    try
      DataSet.DisableControls;
      OldPos := DataSet.GetBookmark;
      DataSet.First;
      PrintHeader(DataSet, X, Y, H);
      Y := Y + H * 2;
      while not DataSet.Eof do
      begin
        PrintRecord(DataSet, X, Y, H);
        Y := Y + H;
        DataSet.Next;
      end;
      DataSet.GotoBookmark(OldPos);
      DataSet.FreeBookmark(OldPos);
    finally
      DataSet.AfterScroll := SaveAfterScroll;
      DataSet.EnableControls;
    end; // try
  end;
end;

You'll need to add some code to handle page breaks.

Rob McDonell
  • 1,309
  • 9
  • 15
0

You can loop into your grid and put it all into your richedit manually. But why reinvent the wheel. Just use a report component. On delphi 7 - delphi2010 you have the rave components installed.

Ravaut123
  • 2,764
  • 31
  • 46
  • It is for a school project witch is dun in class and we may not download or install new components because someone skrewd up the system when he installed a component. Can you give me an example on how to use the rave components please? – Arno Sep 21 '11 at 13:02
  • Self, i don't use Rave. I use ReportBuilder. But Rave Report is normaly on delphi7- delphi2010. example: http://componentace.com/reports-rave-report-delphi.htm But if you don't have the components on de IDE. Then you must loop into your data. – Ravaut123 Oct 03 '11 at 08:56