17
votes

I'm using the edit event on a Kendo grid to show a couple of hidden columns. I'll then hide them again on the save event.

Problem I have is that there doesn't appear to be an event for cancelling edit mode, so the column get screwed up if the user clicks cancel.

Is there an undocumented event for cancel or do I need to find a workaround?

4

4 Answers

20
votes

Basically there is no such "Cancel" event, however you can attach click event on the "Cancel" button in the еdit event of the Grid. Please check the example below:

function onEdit(e) {
   e.container.find(".k-grid-cancel").bind("click", function () {
      //your code here
   })
 }

EDIT: From some time the Grid have "cancel" event which can be used instead of the above solution:

16
votes

I've been looking for an answer to the same question but this didn't work for me. I had a scenario where new and edited records within my grid are validated within my controller and error messages are added to the ModelState's ModelError collection. I had hooked up the grid's datasource error event which then displayed the error message within an alert, and then added the following which reset the changes:

$('#MyGrid').data("kendoGrid").cancelChanges();

It was a neat solution for me because I am using paging and the current page the user is viewing is preserved.

10
votes

Contrary to what the accepted answer states there is in fact a cancel event just like the edit event.

$("#grid").kendoGrid({
    ...
    edit: function(e) {
        alert("edit")
    },
    cancel: function(e) {
        alert("cancel");
    },
    ...
});
0
votes

Try this,

$("#grid").kendoGrid({
 columns: [
  { field: "name" },
  { field: "age" }
 ],
 dataSource: [
  { name: "Jane Doe", age: 30 },
  { name: "John Doe", age: 33 }
 ],
 dataBound: function(e) {
  $("#grid").on("mousedown", ".k-grid-cancel-changes", function (e) {
    //custom logic
  });
 }
});

In dataBound, wire click event for the kendo grid Toolbar cancel button. It will work.