Uses OnCellImageClick
to turn a column’s ImageIndex icon into a per-row action
button - deleting the current record - without a separate button column
or component.
A “trash can” icon in a narrow column, one per row. Clicking it
deletes that record (after a confirmation), without also selecting the
row the way an ordinary click on the column would
(OnCellImageClick fires instead of
OnCellClick, not alongside it - the one deliberate
exception among this unit’s click events).
// FormCreate, after FGrid.Images is assigned:
ColActions.ImageIndex := IDX_TRASH; // TEgColumn.ImageIndex - see TEgColumn
ColActions.FieldName := ''; // Custom Cell column, no field of its own
FGrid.OnCellImageClick := GridCellImageClick;
procedure TfrmOrders.GridCellImageClick(Column: TColumn);
begin
if Column <> ColActions then Exit; // only this one column has an action icon
if MessageDlg('Delete this order?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
Table.Delete;
end;
Row/record parameter needed: by the
time this fires, the dataset is already positioned on the clicked row -
same guarantee OnCellClick itself gives (row repositioning
happens earlier, in MouseDown).ImageIndex icon
counts, never a checkbox and never a real decoded graphic/BLOB image -
only the fallback icon a graphic column shows when its field is
null or fails to decode. A plain icon column, as used here, is the
common case.ImageOnSelectedOnly
or ShowImageOnEmpty,
this event only fires for a click on an icon that was genuinely visible
at the moment of the click - a click that only just selects a
previously-unselected row is treated as an ordinary
OnCellClick instead, since the icon was not showing yet
under ImageOnSelectedOnly at that instant.Column <> ColActions at the top is the simplest guard
when only one column has an action icon; with several action columns,
branch on Column.FieldName or a Tag, the same
way OnBeforeDrawCell
branches by field name.TEgColumn.OnCellImageClick
is the column-scoped equivalent - attach it directly to
ColActions instead of checking
Column <> ColActions in a grid-wide handler, if the
action only ever applies to that one column:
ColActions.OnCellImageClick := ColActionsImageClick;
procedure TfrmOrders.ColActionsImageClick(Column: TColumn);
begin
if MessageDlg('Delete this order?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
Table.Delete;
end;
When both the grid-level and column-level handlers are assigned, the grid-level one runs first.
The same “instead of the native click” pattern for icons elsewhere in
the grid: OnTitleImageClick
for a column’s title image (this one does still let
OnTitleCellClick fire alongside it -
OnCellImageClick is the one exception), OnCornerImageClick
for the fixed corner image, and OnBeforeDrawTitle
to change which image a title shows, mirroring what OnBeforeDrawCell
does for data-cell images.