In my cxGrid I have records grouped by date. However, when a user selects a grouped date and hits delete all the records that were filed under that date get deleted. Is there a way to prevent this ? I would like the records to be deleted individually.
EDIT: I forgot to mention: I am grouping using GroupByBox function of the grid.
You can check gridView.Controller.FocusedRow.IsData
property before delete action. Here is example:
gridView.OptionsData.Deleting = False;
Add code in gridView's event OnKeyUp
:
procedure TmyForm.gridViewKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (Key = VK_DELETE) and (gridView.Controller.FocusedRow <> nil) then
begin
if gridView.Controller.FocusedRow.IsData then
begin
gridView.DataController.DeleteFocused;
end
else
begin
ShowMessage('You can''t delete group records.');
end;
end;
end;
If you use default dataset actions or cxDBNavigator
try this:
gridView.OptionsData.DeleteConfirmation = False;
Add "abort" code in dataset event BeforeDelete
:
procedure TmyForm.DataSetBeforeDelete(DataSet: TDataSet);
begin
if (gridView.Controller.FocusedRow <> nil)
and not gridView.Controller.FocusedRow.IsData then
begin
ShowMessage('You can''t delete group records.');
Abort;
end;
end;
Add "abort" code to code navigator event OnButtonClick
(if you don't want to show delete confirmation):
procedure TmyForm.DBNavigatorButtonsButtonClick(Sender: TObject; AButtonIndex: Integer; var ADone: Boolean);
begin
if (AButtonIndex = NBDI_DELETE)
and (gridView.Controller.FocusedRow <> nil)
and not gridView.Controller.FocusedRow.IsData then
begin
ShowMessage('You can''t delete group records.');
Abort;
end;
end;