Print a TDBGrid [duplicate]

可紊 提交于 2019-12-04 13:01:06

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).

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.

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.

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