CancelEdit does not discard a change

12 posts, 2 answers
  1. Deborah
    Deborah avatar
    154 posts
    Member since:
    Sep 2010

    Posted 14 May 2011 Link to this post

    The Intellisense for CancelEdit says that it closes the active editor and discards the change. It seems to be closing the active editor but NOT discarding the change.

    I tried using the CellValidating event similar to this post:

    http://www.telerik.com/community/forums/winforms/gridview/validating-user-input.aspx

    But e.Cancel also does not discard the change.

    Here is my scenario:
    1) User enters a new value.
    2) In the CellValidating event, the system displays a dialog asking for the reason for the change.
    3) User cancels from that dialog.
    4) Code needs to discard the change and close the active editor.

    Using myGrid.CancelEdit closes the active editor but does not discard the change. Using e.Cancel seems to do nothing.

    How do I end the edit and discard the change to a cell?

    Thanks!
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 16 May 2011 Link to this post

    Hello Deborah,

    I cannot test this now, but if I remember correctly i've ran into this problem before, but the think is this, although the Text of the Cell still changes, the underlying Value of the cell remains unchanged, please try to reset the text value for the cell to the value. I will try to test this out later today and get back to you.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  3. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 16 May 2011 Link to this post

    Hello again,

    If you would like to ask the user if he wants to change something before performing the actual change you can use the following:
    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        var editor = e.ActiveEditor;
        if (editor != null)
        {
            editor.ValueChanging -= new ValueChangingEventHandler(editor_ValueChanging);
            editor.ValueChanging += new ValueChangingEventHandler(editor_ValueChanging);
        }
    }
     
    void editor_ValueChanging(object sender, ValueChangingEventArgs e)
    {
          // ask here
        e.Cancel = true;
    }

    If this is not a good a solution i will try to see what's happening with the CellValidating and the CancelEdit().

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  4. Lee McColm
    Lee McColm avatar
    1 posts
    Member since:
    Jan 2008

    Posted 22 Aug 2011 Link to this post

    Did anyone ever end up looking at what's happening with the CancelEdit call within the CellValidating method?  I have this exact same problem but when I call .CancelEdit() from inside the CellValidating method, the editor doesn't disappear and the grid appears to stay in edit mode.

    I can't use the ValueChanging method that you suggested because I need to code to fire once when they're finished typing the new value and not on every keystroke which happens on the ValueChanging method.
  5. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 25 Aug 2011 Link to this post

    Hi Lee Mccolm,

    Thank you for your question. 

    Currently, it  is not allowed by design to call the CancelEdit method when handling the CellValidating event. However, because this feature seems reasonable, I have added it to our Public Issue Tracking System. We will do our best enable this functionality in our upcoming Service Pack scheduled to the beginning of September.

    I have also updated your Telerik points for this feature request. If you have any further questions, do not hesitate to contact us.
     
    Best wishes,
    Jack
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  6. Answer
    David
    David avatar
    57 posts
    Member since:
    Apr 2011

    Posted 07 Nov 2012 Link to this post

    For future googlers, I've found the 2012 Q3 version (tested) allows CancelEdit to be called from the CellValidating event, so I presume this functionality was added at some time after Jack's post on Aug 25, 2011. Here's my code:

    Private Sub RadGridView1_CellValidating(sender As Object, e As CellValidatingEventArgs) Handles RadGridView1.CellValidating
        If [some condition to cancel editing] Then
            e.Cancel = True
            DirectCast(sender, RadGridView).CancelEdit()
        End If
    End Sub

    Kind regards,
    Dave.
  7. Jared
    Jared avatar
    14 posts
    Member since:
    Oct 2015

    Posted 31 May in reply to David Link to this post

    This works as expected but it also wipes out any ErrorText you set on the row or any cells in the current row.     
  8. Dess
    Admin
    Dess avatar
    2180 posts

    Posted 01 Jun Link to this post

    Hello Jared, 

    Thank you for writing.  

    When you call the CancelEdit method in the CellValidating event, the Row.ErrorText remains. I have attached my sample project for your reference which result is illustrated in the attached gif file. Am I missing something? Could you please specify the exact steps how to reproduce the problem?

    I am looking forward to your reply.

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  9. Jared
    Jared avatar
    14 posts
    Member since:
    Oct 2015

    Posted 01 Jun in reply to Dess Link to this post

    Calling CancelEdit() on the grid causes the CellValidated to fire - disregarding the e.Cancel = true logic.  Add this to your project and you will see the same behavior:

    public RadForm1()
    {
        InitializeComponent();
        radGridView1.CellValidated += RadGridView1OnCellValidated;
    }
     
    private void RadGridView1OnCellValidated(object sender, CellValidatedEventArgs cellValidatedEventArgs)
    {
        cellValidatedEventArgs.Row.ErrorText = "";
    }     
  10. Dess
    Admin
    Dess avatar
    2180 posts

    Posted 02 Jun Link to this post

    Hello Jared, 

    Thank you for writing back. 

    The CellValidating/CellValidated events are always fired when you change the current cell in the grid no matter if the grid is in edit mode or not. That is why the ErrorText is being reset. I would recommend you to clear the ErrorText in the entry point of the CellValidating event and depending on the input, adjust the ErrorText. Here is a sample code snippet:
    private void radGridView1_CellValidating(object sender, Telerik.WinControls.UI.CellValidatingEventArgs e)
    {
        e.Row.ErrorText = "";
        if (e.Value == null || e.Value.ToString() == "")
        {
            e.Cancel = true;
            e.Row.ErrorText = "error";
            this.radGridView1.CancelEdit();
        }
    }

    Thus, the ErrorText will remain although the editor is closed.

    If you are still experiencing any further difficulties, it would be greatly appreciated if you specify in details what is the exact goal that you are trying to achieve. Thus, we would be able to assist you further. Thank you.

    I hope this information helps. If you have any additional questions, please let me know. 

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  11. Jared
    Jared avatar
    14 posts
    Member since:
    Oct 2015

    Posted 02 Jun in reply to Dess Link to this post

    The CellValidated does not fire and should not fire when there is an error in the CellValidating event and e.Cancel = true is set.  But when you set e.Cancel = true followed by .CancelEdit() it causes the CellValidated event to fire when it shouldn't.  

    Here is the link to the .GIF screen recording describing this behavior with your example project:

    https://1drv.ms/i/s!AnwxmkuSjy2nhd4WOAS0OpP-m69wkw

    (I couldn't attach the file due to the 2MB file size limit)

  12. Hristo
    Admin
    Hristo avatar
    1068 posts

    Posted 05 Jun Link to this post

    Hi Jared,

    Thank you for writing.

    The reported scenario is not supported. The CellValidating event is suitable for performing a check for the cell`s value and one should not call the RadGridView1.CancelEdit method as it will interfere with the validation logic.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top