Search results

Disable editing for a particular row/cell in ASP.NET MVC Grid control

22 Oct 2021 / 1 minute to read

You can disable the editing for a particular row by using the ActionBegin event of Grid.

In the below demo, the rows which are having the value for ShipCountry column as Denmark is prevented from editing.

razor
disable-edit.cs
Copied to clipboard
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPaging().Columns(col =>
{
    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(30).Add();
    col.Field("CustomerID").HeaderText("Customer ID").Width(30).Add();
    col.Field("ShipCity").HeaderText("Ship City").Width(30).Add();
    col.Field("ShipCountry").HeaderText("Ship Country").Width(30).Add();

}).EditSettings(edit => edit.AllowEditing(true)).Toolbar(new List<string>() { "Edit", "Cancel", "Update" }).ActionBegin("actionBegin").Render()

<script>
    function actionBegin(args) {
        if (args.requestType === "beginEdit") {
            if (args.rowData.ShipCountry == "Denmark") {
                args.cancel = true;
            }
        }
    }
</script>
Copied to clipboard
public IActionResult Index()
{
    ViewBag.DataSource = OrderDetails.GetAllRecords();
    return View();
}

For batch mode of editing, you can use CellEdit event of Grid. In the below demo, the cells which are having the value as Denmark is prevented from editing.

razor
disable-edit-batch.cs
Copied to clipboard
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowPaging().Columns(col =>
{
    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(30).Add();
    col.Field("CustomerID").HeaderText("Customer ID").Width(30).Add();
    col.Field("ShipCity").HeaderText("Ship City").Width(30).Add();
    col.Field("ShipCountry").HeaderText("Ship Country").Width(30).Add();

}).EditSettings(edit => edit.AllowEditing(true).Mode(Syncfusion.EJ2.Grids.EditMode.Batch)).Toolbar(new List<string>() { "Edit", "Cancel", "Update" }).CellEdit("cellEdit").Render()

<script>
    function cellEdit(args) {
        if (args.value == "Denmark") {
            args.cancel = true;
        }
    }
</script>
Copied to clipboard
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}