How To…Create a Crosstab via Code

Question

“How can I create and configure a crosstab using code?”

Solution

The following example adds a crosstab to the detail band and defines row, column, and value dimensions.

Download: CreateCrosstabViaCode.zip

uses
  ppClass,
  daDataModule,
  ppReport,
  ppCTMain;


procedure TForm1.CreateCrosstabInCode(aReport: TppReport);
var
  lCrossTab: TppCrossTab;
  liRowDimension: Integer;
  liValueDimension: Integer;
begin

  // create crosstab and place in the detail band
  lCrossTab := TppCrossTab.Create(Self);
  lCrossTab.Band := aReport.Detail;

  // assign the datapipeline
  lCrossTab.DataPipeline := daDataModule1.DataViews[0].DataPipelines[0];

  // initialize the available dimensions list based on the datapipeline
  lCrossTab.InitAvailableDimensions;

  // add a row dimension for CustNo
  liRowDimension := lCrossTab.IndexOfAvailableDimension('CustNo');
  lCrossTab.SelectRowDef(liRowDimension);

  // add a row dimension for OrderNo
  liRowDimension := lCrossTab.IndexOfAvailableDimension('OrderNo');
  lCrossTab.SelectRowDef(liRowDimension);

  // add a value dimension for AmountPaid
  liValueDimension := lCrossTab.IndexOfAvailableDimension('AmountPaid');
  lCrossTab.SelectValueDef(liValueDimension);

end;