Hierarchy Batch Edit Saving

1 Answer 212 Views
Grid
Randy
Top achievements
Rank 1
Iron
Randy asked on 26 Sep 2022, 01:16 PM

I have a Hierarchy grid where I need to save both master view and detail view changes using an external button. For each view it calls the OnBatchEditCommand method. 

I want to execute a method after completing all the BatchEditCommands. How can I do it?

 

Code
<script type="text/javascript">
        function OnBatchEditOpening(sender, args) {
            var columnName = args.get_columnUniqueName();

            if (columnName != "AcceptableValues") {
                args.set_cancel(true);
            }
        }

        function savePage(sender, args) {
            var pageIsValid = Page_ClientValidate();
            if (!pageIsValid) {
                args.set_cancel(true);
            }
            args.set_cancel(saveAllChanges());
        }

        function saveAllChanges() {
            var grid2 = $find("<%= RadGrid1.ClientID %>");
             
            var masterTable2 = grid2.get_masterTableView();
            var batchEditManager = grid2.get_batchEditingManager();
            
            var tables = [];
            tables.push(masterTable2);
            tables.push(grid2.get_detailTables()[0]);
            var isDirty = false;
            for (var i = 0; i < tables.length; i++) {
                if (batchEditManager.hasChanges(tables[i])) {
                    isDirty = true;
                    break;
                }
            }
            if (isDirty) {
                batchEditManager.saveAllChanges(tables);
            }
            return isDirty;
        }
    </script>

<telerik:RadGrid ID="RadGrid1" runat="server" RenderMode="Lightweight"
    OnPreRender="RadGrid1_PreRender"
    OnNeedDataSource="RadGrid1_NeedDataSource" OnBatchEditCommand="RadGrid1_BatchEditCommand" MasterTableView-EditMode="Batch" >
        <ClientSettings>
            <ClientEvents OnBatchEditOpening="OnBatchEditOpening" />
        </ClientSettings>
        <MasterTableView Name="Errors" DataKeyNames="ErrorType,ColumnFieldName,Value" EditMode="Batch" AutoGenerateColumns="false" HeirarchyLoadMode="client">
            <BatchEditingSettings  SaveAllHierarchyLevels="true" />
            <Columns>
                        <telerik:GridBoundColumn UniqueName="ErrorType" DataField="ErrorType" HeaderText="Error Type" ></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="ExcelColumnCell" DataField="ExcelColumnCell" HeaderText="Excel Column/Cell"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="ColumnFieldName" DataField="ColumnFieldName" HeaderText="Column/Field Name" ></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="Value" DataField="Value" HeaderText="Value"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="AcceptableValues" DataField="AcceptableValues" HeaderText="Acceptable Values"></telerik:GridBoundColumn>
                     </Columns>
            <DetailTables>
                <telerik:GridTableView DataMember="ErrorDetails" Name="ErrorDetails" DataKeyNames="ErrorType,ColumnFieldName,Value" EditMode="Batch" AutoGenerateColumns="false"
                     ShowHeader="false"
                     ShowFooter="false"
                     AllowPaging="false"
                     AllowFilteringByColumn="false"
                     GridLines="None"
                     BorderStyle="None"
                     BatchEditingSettings-EditType="Cell"
                    HeirarchyLoadMode="client">  
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="ErrorType" MasterKeyField="ErrorType" />
                        <telerik:GridRelationFields DetailKeyField="ColumnFieldName" MasterKeyField="ColumnFieldName" />
                        <telerik:GridRelationFields DetailKeyField="Value" MasterKeyField="Value" />
                     </ParentTableRelation>
                    <Columns>
                        <telerik:GridBoundColumn UniqueName="ErrorType" DataField="ErrorType" HeaderText="Error Type" ></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="ExcelColumnCell" DataField="ExcelColumnCell" HeaderText="Excel Column/Cell"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="ColumnFieldName" DataField="ColumnFieldName" HeaderText="Column/Field Name" ></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="Value" DataField="Value" HeaderText="Value"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="AcceptableValues" DataField="AcceptableValues" HeaderText="Acceptable Values"></telerik:GridBoundColumn>
                     </Columns>
                </telerik:GridTableView>
            </DetailTables>
        </MasterTableView>
    </telerik:RadGrid>
    
    <telerik:RadButton runat="server" ID="RadButton1" Text="Save" OnClientClicking="savePage" OnClick="RadButton1_Click"></telerik:RadButton>

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 29 Sep 2022, 11:20 AM

Hi Amendra,

Yes, you are right that the BatchEditCommand event fires for each GridTableView that is being updated.

In this case, you can define a flag outside of the event, mark the flag if the BatchEditCommand fires and execute custom logic in another later event of the RadGrid (such that will be executed fired per request), e.g. the PreRender exposed by the Grid.

For instance:

bool isGridSaving = false;
protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
{
    isGridSaving = true;
}

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (isGridSaving)
    {
        CustomMethod();
    }
}

protected void CustomMethod()
{

}

Please give it a try and let me know how it goes.

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
Randy
Top achievements
Rank 1
Iron
Answers by
Doncho
Telerik team
Share this question
or