How to stop a delete

136
4
Jump to solution
12-12-2017 11:43 AM
Highlighted
by
Occasional Contributor II

I want to stop or warn a user of the deletion of a feature in the RowDeletedEvent.  When the user tries to delete the field I want to ask them if they are sure.  I want to roll back or something if they don't want to delete the item.

protected static void onRowDeleteEvent(RowChangedEventArgs args)
        {
            try
            {
                MessageBox.Show("Are you Sure", "Are you Sure", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Warning);
                //ROLL BACK HERE?

            }
            catch (Exception e)
            {
                ProMapBlackLogWriter.logError("ProMapBlackModule - onRowDeleteEvent", e);
            }
        }
Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Regular Contributor

Sorry i should have included a more complete example. You can check the guid of the row passed in to prevent reentrancy. This method is really the only place to catch/stop deletes from all sources.

    //Guid _lastEdit = Guid.Empty;
    private void onRowDeletedEvent(RowChangedEventArgs obj)
    {
      if (_lastEdit != obj.Guid)
      {
        //cancel with no notice
        //obj.CancelEdit(() => Task.FromResult(false));

        //cancel with dialog
        obj.CancelEdit("Delete Event\nAre you sure", true);
        _lastEdit = obj.Guid;
      }
    }

View solution in original post

4 Replies
Highlighted
Esri Regular Contributor

You can use the RowChangedEventArgs.CancelEdit method. The options are limited at this time but it was designed for this use case.

obj.CancelEdit("Delete Event\nAre you sure", true);
Highlighted
by
Occasional Contributor II

Where can i call this from?  If I call it from the OnRowDeleteEvent, and then select yes, I get a loop right.  I need to catch when the user hits the delete button, and this is the only place right?

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Sorry i should have included a more complete example. You can check the guid of the row passed in to prevent reentrancy. This method is really the only place to catch/stop deletes from all sources.

    //Guid _lastEdit = Guid.Empty;
    private void onRowDeletedEvent(RowChangedEventArgs obj)
    {
      if (_lastEdit != obj.Guid)
      {
        //cancel with no notice
        //obj.CancelEdit(() => Task.FromResult(false));

        //cancel with dialog
        obj.CancelEdit("Delete Event\nAre you sure", true);
        _lastEdit = obj.Guid;
      }
    }

View solution in original post

Highlighted
by
Occasional Contributor II

I wanted to add my finished code to help others who might want to do this.  The out of box message box didn't looks right, so I made this simple solution to stop a delete, with the help of Sean's code.

public static long CurrentlyDeletingOID = 0;
protected static void onRowDeleteEvent(RowChangedEventArgs args)
{
    try
    {
        long _deletingOID = args.Row.GetObjectID();
        if (CurrentlyDeletingOID != _deletingOID)

            MessageBoxResult mbr = ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Are you sure you want to Delete this Feature?", "Confirm Delete",
                                                                                    MessageBoxButton.YesNo, MessageBoxImage.Warning);

            if (mbr.Equals(MessageBoxResult.Yes))
            {
                //PROCEED with the delete
                args.CancelEdit(() => Task.FromResult(true));

                //Need to set this so that the code doesn't get stuck in loop, no need to reset
                CurrentlyDeletingOID = _deletingOID;
            }
            else
            {
                //CANCEL the Delete
                args.CancelEdit(() => Task.FromResult(false));
            }

            //cancel with dialog using ESRI commands
            //args.CancelEdit("Delete Event\nAre you sure", true);

        }
    }
    catch (Exception e)
    {
        logError("onRowDeleteEvent", e);
    }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍