This is a migrated thread and some comments may be shown as answers.
CancelEdit does not discard a change
11 Answers 224 Views
This is a migrated thread and some comments may be shown as answers.
Deborah
Top achievements
Rank 1
Deborah asked on 14 May 2011, 05:28 PM
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!

11 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 16 May 2011, 08:22 AM
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
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 16 May 2011, 09:53 AM
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
0
Lee McColm
Top achievements
Rank 1
answered on 22 Aug 2011, 09:34 PM
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.
0
Jack
Telerik team
answered on 25 Aug 2011, 09:13 AM
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 >>

0
Accepted
David
Top achievements
Rank 2
answered on 07 Nov 2012, 12:26 PM
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.
0
Jared
Top achievements
Rank 2
answered on 31 May 2017, 08:34 PM
This works as expected but it also wipes out any ErrorText you set on the row or any cells in the current row.     
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 01 Jun 2017, 10:24 AM
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.
0
Jared
Top achievements
Rank 2
answered on 01 Jun 2017, 03:20 PM

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 = "";
}     
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Jun 2017, 10:53 AM
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.
0
Jared
Top achievements
Rank 2
answered on 02 Jun 2017, 02:35 PM

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)

0
Hristo
Telerik team
answered on 05 Jun 2017, 02:25 PM
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.
Asked by
Deborah
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Lee McColm
Top achievements
Rank 1
Jack
Telerik team
David
Top achievements
Rank 2
Jared
Top achievements
Rank 2
Dess | Tech Support Engineer, Principal
Telerik team
Hristo
Telerik team
Share this question
or