CancelEdit does not discard a change

6 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. UI for WinForms is Visual Studio 2017 Ready
  4. 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
  5. 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.
  6. Jack
    Admin
    Jack avatar
    2335 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 >>

  7. 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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready