Search code examples
delphidevexpressdelphi-xe4tcxgrid

cxGrid prevent grouped delete


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.


Solution

  • You can check gridView.Controller.FocusedRow.IsData property before delete action. Here is example:

    1. Disable "auto deleting" feature by set gridView.OptionsData.Deleting = False;
    2. 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:

    1. Disable "confirm delete" feature by set gridView.OptionsData.DeleteConfirmation = False;
    2. 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;
      
    3. 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;