RadGrid BatchEdit Prevent rows from edit based on acondition

12 posts, 1 answers
  1. DRG
    DRG avatar
    13 posts
    Member since:
    Jun 2013

    Posted 06 Jan 2014 Link to this post

    Telerik RadGrid is populated from ObjectDataSource. RadGrid is in BatchEdit mode. Need to prevent a row from editing if a readonly checkbox column is checked.

    <telerik:radgrid id="RadGridTest" runat="server" allowsorting="True" allowpaging="True"
        allowautomaticupdates="True" allowautomaticdeletes="True" allowautomaticinserts="True"
        gridlines="None" pagesize="10" cellspacing="0"
        datasourceid="RadGridTestSource">
        <filtermenu enableimagesprites="False" />
        <pagerstyle mode="NumericPages" />
        <mastertableview width="100%" commanditemdisplay="Top" datasourceid="RadGridTestSource"
            horizontalalign="NotSet" editmode="Batch" autogeneratecolumns="False" datakeynames="TestId"
            overridedatasourcecontrolsorting="true">
        <BatchEditingSettings EditType="Cell" />
         <SortExpressions>
            <telerik:GridSortExpression FieldName="TestId" SortOrder="Descending" />
        </SortExpressions>
        <Columns>                   
            <telerik:GridBoundColumn DataField="TestId" HeaderText="TestId" SortExpression="TestId"
                 UniqueName="TestId" Visible="false">
            </telerik:GridBoundColumn>                                     
            <telerik:GridCheckBoxColumn DataField="EditKey" HeaderStyle-Width="80px" HeaderText="Edit Key"    SortExpression="EditKey" UniqueName="EditKey" ReadOnly="true" >
                    </telerik:GridCheckBoxColumn>                            
        </Columns>         
    </mastertableview>
    </telerik:radgrid>


     protected void RadGridTest_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;          
                if (((PROJ.Object)(((Telerik.Web.UI.GridItem)(dataItem)).DataItem)).EditKey == true)
                {
                    dataItem.Edit = false;
                }          
            }
        }

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 09 Jan 2014 Link to this post

    Hello,

    In order to prevent opening of the editor for a given row when a checkbox on that row is checked you would need some custom JavaScript code. You could use the handler for the OnBatchEditOpening client event and if the input is checked - cancel the event.

    The handler could look similar to the one below:

    function batchEditOpening(sender, args) {
        // forces the creation of the client data items
        sender.get_masterTableView().get_dataItems();
     
        var dataItem = $find(args.get_row().id);
        
        if (dataItem.get_cell("EditKey").getElementsByTagName('input')[0].checked) {
            args.set_cancel(true);
        }
    }


    Regards,
    Viktor Tachev
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. DRG
    DRG avatar
    13 posts
    Member since:
    Jun 2013

    Posted 10 Jan 2014 Link to this post

    <ClientSettings>
      <ClientEvents OnBatchEditOpening="BatchEditOpening" />
    </ClientSettings>


    Error   12  Type 'Telerik.Web.UI.GridClientEvents' does not have a public property named 'OnBatchEditOpening'.     
  5. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 15 Jan 2014 Link to this post

    Hi,

    The error you are seeing is most likely because the project you have is using older version of our controls and the OnBatchEditOpening is not available in it. It is recommended to upgrade to the latest version as it includes new features and improvements.

    Please ensure that you are using the new version of the controls (currently 2013.3.1114) and this error should not be shown.

    Regards,
    Viktor Tachev
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  6. Steven
    Steven avatar
    81 posts
    Member since:
    Dec 2011

    Posted 14 Feb 2014 in reply to Viktor Tachev Link to this post

    This cancels the Row Edit, but the cell_editing fires for every column.

    Although it cancels the Row Editing it still binds the Document Click event that fires the _tryCloseEdits still fires and I get an error in the _getDataControl.

    Is there any way to effectively cancel out of a ROW level or CELL level BATCH edit without refreshing the entire document.

    sender.get_batchEditingManager().cancelChanges(sender.MasterTableView);

    Invokes the Rebind event which triggers a postback
     
  7. Steven
    Steven avatar
    81 posts
    Member since:
    Dec 2011

    Posted 15 Feb 2014 Link to this post

    I was able to stop the even from bubbling up to the RadGrid div container by attaching and inspecting the click on the MasterTableView.

    I still have an outstanding error when editing a ROW and then click on another ROW, the editors accurately swap to the new row, but if I click outside of the RadGrid all together (BODY), then I get an error in the described above with the
    body.onClick
     => _tryCloseEdits
         => _updateCellValue
              => getDataControl
                    => Error (arguments[0]  aka 'o') is null
                          This stems from the $telerik.getElementByClassName(object, 'rgBartchContainer')

    The object is an Editable Cell, but I have a "cell_editing" event that validates the editing cell and cancels the edit so the editor is not populated.


    IE 
    function cell_editing(sender, args) {
        var cell = args.get_cell();
        
        if (cell.className.indexOf("edit") < 0)
              args.set_cancel(true);
    }



    This works just fine when the BATCH EDIT MODE is CELL, but errors out when the mode is ROW.

    My batch editing scenario is that that half of the table cells are editable, but each line item (row) has/defines different restrictions that depict when a certain column can be edited and the Date Column may be editable on row1 and readonly on row2.

    Any suggestions on how I can get the BATCH ROW editing mode to play nice or how to resolve the following.  
    Perhaps  allow the editing but then Hide the Editor and Show the Content in a delayed call (window.setTimeout) so that the editor still exists within the dom?





    }







  8. Steven
    Steven avatar
    81 posts
    Member since:
    Dec 2011

    Posted 15 Feb 2014 in reply to Steven Link to this post

    FYI, I was able to accomplish this with the BATCH ROW.

    Since the _tryCloseEdits had no knowledge that individual cells may have been canceled, I had to move my cancel logic to the a "_getCellDataToOpenEdit" wrapper.

    var proto = Telerik.Web.UI.GridBatchEditing.prototype._getCellDataToOpenEdit;
    var grid = $find(gridId);

    var mgr = grid.get_batchEditingManager();
    mgr._getCellDataToOpenEdit = function (n, m, r) {
         var data = proto.call(this, n, m, r);
         if (data != null && data.cell != null && data.cell.className.indexOf("edit") < 0)
                   return null;
          return data;
    }
  9. shashi
    shashi avatar
    24 posts
    Member since:
    Jan 2016

    Posted 14 Jun Link to this post

     Thanks for this post it helped me a lot .

    But I have an additional requirement . Is it possible to disable rest of the cell in a row based on the value entered in an editor cell. I have dropdown list in editor template for all cells in row.

     

    Thanks in advance .

  10. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 15 Jun Link to this post

    Hi Shashi,

    The approach you can use to implement the behavior will be similar to the one that is suggested here.

    Handle the OnBatchEditOpening event and check what is the value in the specific cell. Then based on that you can cancel the event - thus, preventing the user from editing values in the grid.

    Regards,
    Viktor Tachev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  11. shashi
    shashi avatar
    24 posts
    Member since:
    Jan 2016

    Posted 15 Jun in reply to Viktor Tachev Link to this post

    Thanks for prompt reply.

    If I do that it doesn't allow to edit complete row.

    In the row I have 30 cells if user enters some code for example T in the 5th cell then user shouldn't be able to edit cells from 6 to 30th but he can edit first 5 cells.

    Please help me with this.

     

  12. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 20 Jun Link to this post

    Hello,

    To prevent editing only for some of the cells you would need to change the if statement. You would need to check what is the value for the cell that specifies whether the user can edit the grid.

    Also you would need to check what is the cell that the user clicked on. If it is one of the "readonly" cells - cancel the event.

    function batchEditOpening(sender, args) {
        var grid = sender;
        var masterTable = grid.get_masterTableView();
        var batchEditManager = grid.get_batchEditingManager();
     
        masterTable.get_dataItems();
     
        var dataItem = $find(args.get_row().id);
        debugger;
     
        // check the value in the cell that determines if the user can edit the fields
        // the second condition checks what is the cell that the user is trying to edit
        if ((dataItem.get_cell("ColumnUniqueName").innerText == "T") && (args.get_columnUniqueName("NonEditableColumnUniqueName") == "NonEditableColumn1")) {
            args.set_cancel(true);
        }
    }


    Regards,
    Viktor Tachev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  13. shashi
    shashi avatar
    24 posts
    Member since:
    Jan 2016

    Posted 20 Jun in reply to Viktor Tachev Link to this post

    Thanks for the reply.

    I got it working.

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017