Einzelnen Beitrag anzeigen

wendelin

Registriert seit: 29. Dez 2010
Ort: Nürnberg
121 Beiträge
 
Delphi 7 Enterprise
 
#3

AW: Alternierende Farben in TDBGrid

  Alt 23. Dez 2014, 11:37
Hallo,
Hier mein Source-Code:
Delphi-Quellcode:
unit QRNeu;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, QuickRpt, QRCtrls, DBGrids, nuetzlFunctions1, QRExport;

type
  TGridReport = class(TForm)
    GridRep : TQuickRep;
    DetailBand1: TQRBand;
    QRTextFilter1: TQRTextFilter;
    QRCSVFilter1: TQRCSVFilter;
    QRHTMLFilter1: TQRHTMLFilter;
    procedure GridRepPreview(Grid : TDBGrid);
    procedure DetailBand1BeforePrint(Sender: TQRCustomBand;
      var PrintBand: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  GridReport: TGridReport;

implementation

uses DataMod2, DataMod1;

{$R *.dfm}

(******************************************************************************)
 procedure TGridReport.GridRepPreview(Grid : TDBGrid);
(******************************************************************************)
var
  i, CurrentLeft, CurrentTop : integer;
  MySQLString : STRING;

begin
  GridRep.Dataset:=Grid.DataSource.DataSet;

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

  if not GridRep.Bands.HasDetail then // wird normalerweise NICHT
    GridRep.Bands.HasDetail:=true; // benötigt !!

  if not GridRep.Bands.HasPageFooter then // NEU
    GridRep.Bands.HasPageFooter := TRUE;

  MySQLString := DataModule2.IBQ1.Text; // NEU

  GridRep.Bands.ColumnHeaderBand.Height:= Abs(Grid.TitleFont.Height) + 10;
  GridRep.Bands.DetailBand.Height := Abs(Grid.Font.Height) + 10;
  GridRep.Bands.PageFooterBand.Height := Abs(Grid.Font.Height) + 10;

  CurrentLeft := 12;
  CurrentTop := 4; // 6

  Grid.DataSource.DataSet.DisableControls; // don't flicker !!

  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 - 10; // korr. !
        Caption := Grid.Columns[i].Title.Caption;
        Font.Style := [fsBold,fsUnderline];
      end;

      {Create Detail with QRDBText}
      with TQRDBText.Create(GridRep.Bands.DetailBand) do
      begin
        Parent := GridRep.Bands.DetailBand;
        Color := GridRep.Bands.DetailBand.Color;
        Transparent := TRUE; // NEU für Farbwechsel des DetailBandes
        Left := CurrentLeft;
        Top := CurrentTop;
        Alignment := taLeftJustify; // NEU / Alt -> Grid.Columns[i].Alignment;
        AutoSize := FALSE;
        AutoStretch := FALSE; // korr. !
        WordWrap := FALSE; // NEU
        Width := Grid.Columns[i].Width;
        Dataset := GridRep.Dataset;
        DataField := Grid.Fields[i].FieldName;
        CurrentLeft := CurrentLeft + (Grid.Columns[i].Width) + 15;
      end;
    end;

    {Create SysData / Datum-Zeit}
    with TQRSysData.Create(GridRep.Bands.PageFooterBand) do
    begin
      Parent := GridRep.Bands.PageFooterBand;
      Color := clWhite;
      Left := 10;
      Top := CurrentTop;
      Alignment := taLeftJustify;
      Data := qrsDateTime;
      Font.Charset := DEFAULT_CHARSET;
      Font.Color := clWindowText;
      Font.Height := -13;
      Font.Name := 'Arial';
      Font.Style := [fsBold];
    end;
    
    {Create Label / SQL-Statement}
    with TQRLabel.Create(GridRep.Bands.PageFooterBand) do
    begin
      Parent := GridRep.Bands.PageFooterBand;
      Color := clWhite;
      Left := 170;
      Top := CurrentTop;
      Alignment := taLeftJustify;
      Caption := MySQLString; // akt. SQLSTRING
      Font.Charset := DEFAULT_CHARSET;
      Font.Color := clWindowText;
      Font.Height := -13;
      Font.Size := 6;
      Font.Name := 'Small Fonts';
      Font.Style := [fsBold]; // [fsBold,fsUnderline];
    end;

    {Create Label / Seite :}
    with TQRLabel.Create(GridRep.Bands.PageFooterBand) do
    begin
      Parent := GridRep.Bands.PageFooterBand;
      Color := clWhite;
      Left := 948;
      Top := CurrentTop;
      Alignment := taLeftJustify;
      Caption := 'SEITE :';
      Font.Charset := DEFAULT_CHARSET;
      Font.Color := clWindowText;
      Font.Height := -13;
      Font.Name := 'Arial';
      Font.Style := [fsBold];
    end;

    {Create SysData / Seitennummer}
    with TQRSysData.Create(GridRep.Bands.PageFooterBand) do
    begin
      Parent := GridRep.Bands.PageFooterBand;
      Color := clWhite;
      Left := 1024;
      Top := CurrentTop;
      Alignment := taLeftJustify;
      Data := qrsPageNumber;
      Font.Charset := DEFAULT_CHARSET;
      Font.Color := clWindowText;
      Font.Height := -13;
      Font.Name := 'Arial';
      Font.Style := [fsBold];
    end;

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

  finally
    // After all, FREE it NEU!
    GridRep.Bands.ColumnHeaderBand.Free; // wichtig sonst eListError
    GridRep.Bands.DetailBand.DestroyComponents; // SEHR WICHTIG !!
    GridRep.Bands.PageHeaderBand.Free; // wichtig sonst eListError
    GridRep.Bands.PageFooterBand.Free; // sonst Darstellungsfehler
    with Grid.DataSource.DataSet do
    begin
      EnableControls;
    end;
  end;
end;

(******************************************************************************)
 procedure TGridReport.DetailBand1BeforePrint(Sender: TQRCustomBand;
(******************************************************************************)
var PrintBand: Boolean);
begin
  If GridReport.DetailBand1.Color = clMoneyGreen THEN
     GridReport.DetailBand1.Color := clWhite ELSE
     GridReport.DetailBand1.Color := clMoneyGreen;
end;

end.
Ich weiss allerdings nicht ob sich QuickReport mit Deinem Delphi XE Verträgt!
Wenn nicht schreib mir, ich werde dann versuchen Dein Problem mit dem DB-Grid zu lösen.

Frohe Weihnachten
Wendelin
Miniaturansicht angehängter Grafiken
beispquickrep.jpg  
Wolfgang
  Mit Zitat antworten Zitat