Uses OnCellImageClick
to turn an icon shown on every row into an action button, here deleting
the record, without a button column or any other control.
A narrow column at the right shows a trash icon on each row. A click or tap on the icon asks for confirmation and deletes that record. A click elsewhere in the row behaves as usual.
Add a column with no FieldName and set it up in the
Object Inspector or in code:
// FormCreate
ColDelete := Grid1.Columns.Add;
ColDelete.Title.Caption := '';
ColDelete.Width := 32;
ColDelete.ImageIndex := 1; // the trash icon in ImageList1
ColDelete.ImageAlignment := TTextAlign.Center;
ColDelete.ShowImageOnEmpty := True; // the column has no text at all
ColDelete.Sortable := fscNone;
ColDelete.OnCellImageClick := ColDeleteImageClick;
Besides EgFireGrid: System.UITypes
(TTextAlign, TMsgDlgType,
TMsgDlgBtn, mrYes),
FMX.DialogService (TDialogService) and the
unit of the dataset component (here FireDAC.Comp.Client,
for Query1).
procedure TForm1.ColDeleteImageClick(Sender: TObject; Column: TEgFireGridColumn; RowIndex: Integer);
begin
TDialogService.MessageDialog('Delete ' + Query1.FieldByName('Name').AsString + '?',
TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], TMsgDlgBtn.mbNo, 0,
procedure(const AResult: TModalResult)
begin
if AResult = mrYes then
Query1.Delete;
end);
end;
OnCellImageClick replaces
OnCellClick for that click, and never opens an
editor, so the action does not interfere with the other click
handlers.ShowImageOnEmpty is needed because a
column with no field has an empty text in every row, and by default an
empty cell shows no icon.Column. With the grid’s OnCellImageClick
instead, test Column = ColDelete first.The titles and the corner have icons too: TitleImageIndex
with OnTitleImageClick
(a filter menu for the column, for instance) and CornerImageIndex
with OnCornerImageClick
(a menu of grid-wide actions). OnBeforeDrawCell
can show the icon only on some rows.