在我的cxGrid中,我有按日期分组的记录。但是,当用户选择分组日期并点击删除时,在该日期之前归档的所有记录都将被删除。有办法防止这种情况吗?我希望单独删除记录。
编辑:我忘了提及:我正在使用网格的GroupByBox函数进行分组。答案 0 :(得分:1)
您可以在删除操作之前检查gridView.Controller.FocusedRow.IsData
属性。这是一个例子:
gridView.OptionsData.Deleting = False;
在gridView的事件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;
如果您使用默认数据集操作或cxDBNavigator
,请尝试以下操作:
gridView.OptionsData.DeleteConfirmation = False;
在数据集事件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;
将“中止”代码添加到代码导航器事件OnButtonClick
(如果您不想显示删除确认):
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;