0

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.

user763539
  • 3,509
  • 6
  • 44
  • 103
  • Where is your code? Are you using the grid in bound or unbound mode? – Andy_D May 29 '14 at 14:45
  • bound mode of course ... – user763539 May 29 '14 at 14:47
  • If user selected group and pressed Del, it actually means that he is trying to delete the group (all items). Anyway, if you are not happy with default behavior, you can disable Delete command for grid (see .OptionData of view) and implement it as you want - for example assign action to some toolbar item and delete what you want. – Andrei Galatyn May 29 '14 at 16:42
  • so that means I can not actually prevent the group deletion ? Any way I can detect group selection for deletion so I can warn user ? – user763539 May 30 '14 at 01:21

1 Answers1

1

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;
    
JayDi
  • 1,037
  • 15
  • 24
  • that is very nice but I need to retain the option of deleting records in the grid. – user763539 Sep 13 '15 at 22:06
  • problem with this is that I am using the built in navigator of the grid. If I prevent deletion then user must use keyboard to delete the record because the delete glyph is grayed out. – user763539 Sep 13 '15 at 22:15
  • I'm added tips for dataset and date navigator's code. Read second part of answer. – JayDi Sep 14 '15 at 02:23
  • [dcc32 Error] DataModule.pas(179): E2029 ')' expected but identifier 'Controller' found ----[dcc32 Error] DataModule.pas(179): E2003 Undeclared identifier: 'cxGrid1DBTableView1'---This is using your second suggestion... – user763539 Sep 14 '15 at 16:24
  • You must add uses clause with form's unit (it must contain grid) to DataModule.pas like: "uses unit1" – JayDi Sep 14 '15 at 19:55
  • And use code like that: form1.cxGrid1DBTableView1.Controller.FocusedRow – JayDi Sep 14 '15 at 19:56