Skip to main content

Managing Grid Menus

  • 3 minutes to read

Like every VCL control the ExpressQuantumGrid control supports popup menus. But unlike a standard VCL control, the ExpressQuantumGrid control provides advanced popup menu support. This includes:

To add a menu item to a built-in popup menu, do the following:

  1. Define a menu operation associated with the menu item.

  2. Define a set of operations available for a particular grid element type and include the created menu operation into this operation set.

  3. Define a popup menu that provides the specified operation set.

  4. Register the popup menu.

The following code demonstrates how to add a menu item that toggles merging column cells to the built-in column header popup menu.

uses
  ..., cxGridStdPopupMenu, cxGridMenuOperations, cxGridHeaderPopupMenuItems, cxGridCustomPopupMenu, cxGridPopupMenu;
  // Represents a column header menu operation associated with the menu item.
  // To provide an image for the menu item override the GetImageResourceName
  // function to return the name of the image from a resource file.
  // To link a resource file use the {$R } compiler directive. Note that this
  // file should be in the search path.
  TcxGridCellMergingMenuOperation = class(TcxGridHeaderPopupMenuOperation)
  protected
    procedure Execute(Sender: TObject); override;
    function GetDown: Boolean; override;
    function GetEnabled: Boolean; override;
  public
    constructor Create; override;
  end;
  // Represents a column header menu operation set that extends the built-in
  // operation set with a new menu operation
  TcxGridNewHeaderPopupMenuOperations = class(TcxGridHeaderPopupMenuOperations)
  protected
    procedure AddItems; override;
  end;
  // Represents a popup menu that includes the created operation set
  TcxGridNewHeaderMenu = class(TcxGridStdHeaderMenu)
  protected
    function GetOperationsClass: TcxGridPopupMenuOperationsClass; override;
  end;
// ...
implementation
// ...
// ...
resourcestring
  cxCellMerging = 'Merged Cells';
// Toggles a column's cell merging option when clicking the menu item
procedure TcxGridCellMergingMenuOperation.Execute(Sender: TObject);
begin
  inherited;
  HitColumn.Options.CellMerging := not HitColumn.Options.CellMerging;
end;
// Determines whether a check is displayed to indicate whether the cell
// merging option is in effect for the column
function TcxGridCellMergingMenuOperation.GetDown: Boolean;
begin
  Result := HitColumn.Options.CellMerging;
end;
// The menu item is always enabled
function TcxGridCellMergingMenuOperation.GetEnabled: Boolean;
begin
  Result := True;
end;
// Creates a menu item and specifies its caption
constructor TcxGridCellMergingMenuOperation.Create;
begin
  inherited Create;
  ResCaption := cxCellMerging;
end;
// A menu item associated with the menu operation is added as the last
// menu item
procedure TcxGridNewHeaderPopupMenuOperations.AddItems;
begin
  inherited AddItems;
  AddItem(TcxGridCellMergingMenuOperation).BeginGroup := True;
end;
// Specifies the operation set for the column header popup menu
function TcxGridNewHeaderMenu.GetOperationsClass: TcxGridPopupMenuOperationsClass;
begin
  Result := TcxGridNewHeaderPopupMenuOperations;
end;
// ...
initialization
// ...
  // Registers the column header popup menu
  RegisterPopupMenuClass(TcxGridNewHeaderMenu, [gvhtColumnHeader], TcxGridTableView);

Note

Execute the RegisterPopupMenuClass method in the initialization section of the unit to guarantee that your popup menu is always registered.