Cancel change for a specific Row in RadGrid in Batch Mode

1 Answer 233 Views
Grid
Franz
Top achievements
Rank 1
Iron
Iron
Franz asked on 21 Jun 2021, 03:09 PM

Hi,

I would like to know if it is possible to cancel changes for specific rows in RadGrid in Batch Mode. I added a button for each row to cancel the editing operation but when i click on it, it cancel all changes and lose information of other cells that were modified.

i hope you can help me to solve this

Regards

 

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 24 Jun 2021, 11:01 AM

Hi Franz,

I am afraid that the desired behavior goes beyond the built-in capabilities of RadGrid.

Still, you can try to implement the desired with some custom JavaScript and the help of the Batch Editing Client-side API.

For instance, you can use the internal property _changes exposed by the BatchEditingManager to get the original values of the changed cells and revert them programmatically.

And one idea you can try:

Use a template column with "revert changes" button for each GridItem. You can pass the item index to the button's ClientClicked event handler:

<telerik:GridTemplateColumn UniqueName="revertChangesColumn">
    <ItemTemplate>
        <telerik:RadButton runat="server" ID="RadButton1" Text="Revert Row Changes" AutoPostBack="false" OnClientClicked='<%# "function (s,a){ revertRowChanges(s,a,"+"\""+Container.ItemIndex+"\""+");}" %>'></telerik:RadButton>
    </ItemTemplate>
</telerik:GridTemplateColumn>

JavaScript code:

function revertRowChanges(sender, args, itemIndex) {
    //get a reference to the RadGrid, MasterTable, and BatchEditing manager
    var grid = $find("<%= RadGrid1.ClientID %>");
    var masterTable = grid.get_masterTableView();
    var batman = grid.get_batchEditingManager();

    //get the _changes object with the currently done changes
    var itemChanges = batman._changes[masterTable.get_id()][itemIndex];
    if (itemChanges) {
        //get the current data Item by its itemIndex
        var dataItem = getItemByIndex(masterTable, itemIndex);

        //loop through the column names to define the cells that are modified
        var columns = masterTable.get_columns();
        for (var i = 0; i < columns.length; i++) {
            var colName = columns[i].get_uniqueName();

            //if the cell has changed, change the cell value ot the original one
            if (itemChanges[colName]) {
                var originalValue = itemChanges[colName].originalValue;
                batman.changeCellValue(dataItem.get_cell(colName), originalValue)
            }
        }
    }
}

function getItemByIndex(tableView, ItemIndex) {
    var dataItems = tableView.get_dataItems();
    for (var i = 0; i < dataItems.length; i++) {
        if (dataItems[i].get_itemIndex() == ItemIndex)
            return dataItems[i];
    }
    return;
}

Please note that this is a custom implementation and its future maintenance and further modification falls out of the RadGrid supported features.

I hope you will find this information useful.

Kind regards,
Doncho
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Franz
Top achievements
Rank 1
Iron
Iron
Answers by
Doncho
Telerik team
Share this question
or