This is a migrated thread and some comments may be shown as answers.

RadGrid BatchEdit Prevent rows from edit based on acondition

14 Answers 570 Views
Grid
This is a migrated thread and some comments may be shown as answers.
DRG
Top achievements
Rank 1
DRG asked on 07 Jan 2014, 12:56 AM
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;
            }          
        }
    }

14 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 09 Jan 2014, 02:42 PM
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.
0
DRG
Top achievements
Rank 1
answered on 10 Jan 2014, 06:02 PM
<ClientSettings>
  <ClientEvents OnBatchEditOpening="BatchEditOpening" />
</ClientSettings>


Error   12  Type 'Telerik.Web.UI.GridClientEvents' does not have a public property named 'OnBatchEditOpening'.     
0
Accepted
Viktor Tachev
Telerik team
answered on 15 Jan 2014, 10:06 AM
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.
0
Steven
Top achievements
Rank 1
answered on 14 Feb 2014, 06:47 AM
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
 
0
Steven
Top achievements
Rank 1
answered on 16 Feb 2014, 04:01 AM
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?





}







0
Steven
Top achievements
Rank 1
answered on 16 Feb 2014, 04:47 AM
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;
}
0
shashi
Top achievements
Rank 1
answered on 14 Jun 2016, 09:59 PM

 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 .

0
Viktor Tachev
Telerik team
answered on 15 Jun 2016, 11:13 AM
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.
0
shashi
Top achievements
Rank 1
answered on 15 Jun 2016, 02:51 PM

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.

 

0
Viktor Tachev
Telerik team
answered on 20 Jun 2016, 08:35 AM
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.
0
shashi
Top achievements
Rank 1
answered on 20 Jun 2016, 03:53 PM

Thanks for the reply.

I got it working.

0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 28 May 2019, 08:28 PM

Is possible instead of just cancelling focus (args.set_cancel(true);) move to the next cell?

Thank you

0
Attila Antal
Telerik team
answered on 31 May 2019, 02:48 PM
Hi David,

Yes, it is possible using the Batch Ediing Client-Side APIs. If you access the next cell and call the openCellForEdit method of the BatchEditManager, this will automatically close the previous cell that was edited.

Related articles recommended to achieve this scenario:
Kind regards,
Attila Antal
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
David
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 31 May 2019, 02:49 PM

Appreciate the input.

Thank you

Tags
Grid
Asked by
DRG
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
DRG
Top achievements
Rank 1
Steven
Top achievements
Rank 1
shashi
Top achievements
Rank 1
David
Top achievements
Rank 1
Iron
Iron
Veteran
Attila Antal
Telerik team
Share this question
or