Skip to main content

How to Display Underlying Records

  • 3 minutes to read

The following example shows how to display the records from the control’s underlying data source which correspond to a particular cell. A new form with our ExpressQuantumGrid control that displays these records in an unbound TableView is opened when a particular cell in the ExpressPivotGrid control is double-clicked. Note that the ExpressQuantumGrid is a separate product.

The following image shows a sample ExpressPivotGrid control which is bound to the Orders table that is shipped with the control’s demos.

Double-clicking the fourth cell in the January 2002 column will invoke the form with the grid which lists all the payments made in January 2002 by Visa cards.

uses
  ... cxCustomData, cxGridTableView, ...
// This form contains the ExpressQuantumGrid with an unbound TableView.
TForm2 = class(TForm)
  cxGrid1Level1: TcxGridLevel;
  cxGrid1: TcxGrid;
  cxGrid1TableView1: TcxGridTableView;
end;
// Creates a drilldown data source for a particular cross cell and displays its data in a form.
procedure cxShowDrillDownDataSource(ACrossCell: TcxPivotGridCrossCell);
  // Creates columns in the grid's TableView for all the pivot grid fields.
  procedure CreateColumns(APivotGrid: TcxCustomPivotGrid; AGridView: TcxGridTableView);
  var
    I: Integer;
    AField: TcxPivotGridField;
  begin
    for I := 0 to APivotGrid.FieldCount - 1 do
    begin
      AField := APivotGrid.Fields[I];
      with AGridView.CreateColumn do
      begin
        Caption := AField.Caption;
        Visible := AField.Visible;
        Hidden := AField.Hidden;
      end;
    end;
  end;
var
  AForm: TForm2;
  ADataSource: TcxCustomDataSource;
begin
  AForm := TForm2.Create(nil);
  try
    CreateColumns(ACrossCell.PivotGrid, AForm.cxGrid1TableView1);
    ADataSource := ACrossCell.CreateDrillDownDataSource;
    try
      // Links a drilldown data source to the grid's TableView.
      AForm.cxGrid1TableView1.DataController.CustomDataSource := ADataSource;
      AForm.ShowModal;
    finally
      ADataSource.Free;
    end;
  finally
    AForm.Free;
  end;
end;
// The pivot grid's OnDblClick event handler.
procedure TForm1.cxDBPivotGrid1DblClick(Sender: TObject);
var
  ACrossCell: TcxPivotGridCrossCell;
begin
  with cxDBPivotGrid1.HitTest do
  begin
    // Handles double-clicks on data cells
    if HitAtDataCell then
    begin
      // Determines a cross cell which corresponds to the data cell being clicked.
      ACrossCell := TcxPivotGridDataCellViewInfo(HitObject).CrossCell;
      cxShowDrillDownDataSource(ACrossCell);
    end;
  end;
end;