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
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
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
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.
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 >>
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.
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
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 =
""
;
}
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
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)
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