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:
Four built-in grid menus (column header popup menu, footer popup menu, group row popup menu, and Group By box popup menu) provided by the TcxGridPopupMenu component. These menus allow end-users to easily sort, group or align row and cell values, show and hide grid columns, customize column or group summaries at runtime, etc.
An unlimited number of custom popup menus that can be attached to any grid element (cell, column, band, etc.)
The ability to add custom menu items (both standard VCL menu items and menu items from the ExpressBars Suite) to built-in popup menus.
To add a menu item to a built-in popup menu, do the following:
Define a menu operation associated with the menu item.
Define a set of operations available for a particular grid element type and include the created menu operation into this operation set.
Define a popup menu that provides the specified operation set.
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.