Search results

Editing

The Grid component has options to dynamically insert, delete and update records. Editing feature requires a primary key column for CRUD operations. To define the primary key, set Columns.IsPrimaryKey to true in particular column.

You can start the edit action either by double clicking the particular row or by selecting the required row and click on Edit button in the toolbar. Similarly, you can add a new record to grid either by clicking on Add button in the toolbar or on an external button which is bound to invoke the addRecord method of the grid, Save and Cancel while in edit mode is possible using respective toolbar icon in grid.

Deletion of the record is possible by selecting the required row and click on Delete button in the toolbar.

tagHelper
edit.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="315" allowPaging="true">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}
  • If Columns.IsIdentity is enabled, then it will be considered as a read-only column when editing and adding a record.
  • You can disable editing for a particular column, by specifying Columns.AllowEditing to false.

Toolbar with edit option

The grid toolbar has the built-in items to execute Editing actions. You can define this by using the Toolbar property.

tagHelper
edittoolbar.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
            <e-grid-columns>
                <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
                <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
                <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
                <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
            </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Cell edit type and its params

The Columns.EditType is used to customize the edit type of the particular column. You can set the Columns.EditType based on data type of the column.

  • NumericTextBox component for integers, double, and decimal data types.

  • TextBox component for string data type.

  • DropDownList component for list data type.

Also, you can customize model of the Columns.EditType through the Columns.Edit.Params.

The following table describes cell edit type and their corresponding edit params of the column.

Component Example
NumericTextBox params: { decimals: 2, value: 5 }
TextBox -
DropDownList params: { value: ‘Germany’ }
tagHelper
celleditparams.cs
@{

    var editParams = new { params = new {value ="Germany"};};
}

<ejs-grid id="Grid" dataSource="ViewBag.DataSource" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120" ></e-grid-column>                               
        <e-grid-column field="OrderDate" headerText="Order Date" width="150" edit=editParams >            
    </e-grid-column>
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
        {
            var orders = OrdersDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

If edit type is not defined in the column, then it will be considered as the stringedit type (Textbox component) .

Cell Edit Template

The cell edit template is used to add a custom component for a particular column by invoking the following functions:

  • create - It is used to create the element at the time of initialization.

  • write - It is used to create the custom component or assign default value at the time of editing.

  • read - It is used to read the value from the component at the time of save.

  • destroy - It is used to destroy the component.

tagHelper
celledittemplate.cs
<ejs-grid id="Grid" dataSource="ViewBag.datasource" height="315" allowPaging="true">
        <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
        <e-grid-columns>
            <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
            <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>   
            <e-grid-column field="OrderDate" headerText="Order Date" format="yMd" edit="@(new {create = "create", read = "read", destroy = "destroy", write = "write"})" width="120"></e-grid-column>
            <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
            <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
        </e-grid-columns>
    </ejs-grid>
    
    
    <script>
        var elem;
        var dObj;
        function create(args) {
            elem = document.createElement('input');
            return elem;
        }
        function write(args) {
            dObj = new ej.calendars.DatePicker({
                value: new Date(args.rowData[args.column.field]),
                placeholder: 'Select DateTime'
            });
            dObj.appendTo(elem);
        }
    
        function destroy() {
            dObj.destroy();
        }
        function read(args) {
            return dObj.value;
        }
    </script>
public IActionResult Index()
        {
            var orders = OrdersDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Edit Modes

Grid supports the following types of edit modes, they are:

  • Normal
  • Dialog
  • Batch

Normal

In Normal edit mode, when you start editing the currently selected record is changed to edit state. You can change the cell values and save edited data to the data source. To enable Normal edit, set the EditSettings.Mode as Normal.

tagHelper
inline.cs
<ejs-grid id="Grid" dataSource="ViewBag.dataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update","Cancel" })">
            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal"></e-grid-editSettings>
            <e-grid-columns>
                <e-grid-column field="OrderID" headerText="Order ID" validationRules="@(new { required=true})" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>
                <e-grid-column field="CustomerID" headerText="Customer ID" validationRules="@(new { required=true, minLength=3})" type="string" width="120"></e-grid-column>
                <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>
                <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
            </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Normal edit mode is default mode of editing.

Dialog

In dialog edit mode, when you start editing the currently selected row data will be shown on a dialog. You can change the cell values and save edited data to the data source. To enable Dialog edit, set the EditSettings.Mode as Dialog.

tagHelper
dialog.cs
<ejs-grid id="Grid" dataSource="ViewBag.dataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update","Cancel" })">
            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Dialog"></e-grid-editSettings>
            <e-grid-columns>
                <e-grid-column field="OrderID" headerText="Order ID" validationRules="@(new { required=true})" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>
                <e-grid-column field="CustomerID" headerText="Customer ID" validationRules="@(new { required=true, minLength=3})" type="string" width="120"></e-grid-column>
                <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>
                <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
            </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Batch

In batch edit mode, when you double-click on the grid cell, then the target cell changed to edit state. You can bulk save (added, changed and deleted data in the single request) to data source by click on the toolbar’s Update button or by externally calling the batchSave method. To enable Batch edit, set the EditSettings.Mode as Batch.

tagHelper
batch.cs
<ejs-grid id="Grid" dataSource="ViewBag.dataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update","Cancel" })">
            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Batch"></e-grid-editSettings>
            <e-grid-columns>
                <e-grid-column field="OrderID" headerText="Order ID" validationRules="@(new { required=true})" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>
                <e-grid-column field="CustomerID" headerText="Customer ID" validationRules="@(new { required=true, minLength=3})" type="string" width="120"></e-grid-column>
                <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>
                <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
            </e-grid-columns>
 </ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Dialog/Inline template

The dialog template editing provides an option to customize the default behavior of dialog editing. Using the dialog template, you can render your own editors by defining the EditSettings.Mode as Dialog/Inline and EditSettings.Template as SCRIPT element ID or HTML string which holds the template.

In some cases, you need to add the new field editors in the dialog which are not present in the column model. In that situation, the template will help you to customize the default edit dialog.

In the below code example, demonstrate the usage of binding a partial view in the dialog template.

tagHelper
addpatial.cs
dialog.cs
dialogtemplatemodel.cs
editpartial.cs
@{
    ViewData["Title"] = "DialogTemplate";
}

@section ControlsSection{
    <div class="control-section">
        <ejs-grid id="Grid" dataSource="ViewBag.dataSource" actionComplete="actionComplete" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })" allowPaging="true">
            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Dialog" template='#dialogtemplate'></e-grid-editSettings>
            <e-grid-pagesettings pageCount="5 "></e-grid-pagesettings>
            <e-grid-columns>
                <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" validationRules="@(new { required=true})" textAlign="Right" width="120"></e-grid-column>
                <e-grid-column field="CustomerID" headerText="Customer Name" validationRules="@(new { required=true})" width="150"></e-grid-column>
                <e-grid-column field="ShipCountry" headerText="Ship Country" editType="dropdownedit" width="150"></e-grid-column>
            </e-grid-columns>
        </ejs-grid>
    </div>

    <script id='dialogtemplate' type="text/x-template">
        <div id="dialogTemp">
        </div>
    </script>
    
    <script>
        function actionComplete(args) {
            if (args.requestType === 'beginEdit') {
                var ajax = new ej.base.Ajax({
                    url: "/Grid/EditPartial", //render the partial view
                    type: "POST",
                    contentType: "application/json",
                    data: JSON.stringify({ value: args.rowData })
                });
                ajax.send().then(function (data) {
                    appendElement(data, args.form); //Render the edit form with selected record
                    args.form.elements.namedItem('CustomerID').focus();
                }).catch(function (xhr) {
                    console.log(xhr);
                });
            }
            if (args.requestType === 'add') {
                var ajax = new ej.base.Ajax({
                    url: "/Grid/AddPartial", //render the partial view
                    type: "POST",
                    contentType: "application/json",
                    data: JSON.stringify({ value: args.rowData })
                });
                ajax.send().then(function (data) {
                    appendElement(data, args.form); //Render the edit form with selected record
                    args.form.elements.namedItem('OrderID').focus();
                }).catch(function (xhr) {
                    console.log(xhr);
                });
            }
        }

        function appendElement(elementString, form) {
            form.querySelector("#dialogTemp").innerHTML = elementString;
            var script = document.createElement('script');
            script.type = "text/javascript";
            var serverScript = form.querySelector("#dialogTemp").querySelector('script');
            script.textContent = serverScript.innerHTML;
            document.head.appendChild(script);
            serverScript.remove();
        }
    </script>
@using Syncfusion.EJ2

<div>
    <div class="form-row">
        <div class="form-group col-md-6">
            <div class="e-float-input e-control-wrapper">
                @Html.TextBox("OrderID")
                <span class="e-float-line"></span>
                @Html.Label("OrderID", "Order ID", new { @class = "e-float-text e-label-top" })
            </div>
        </div>
        <div class="form-group col-md-6">
            <div class="e-float-input e-control-wrapper">
                @Html.TextBox("CustomerID")
                <span class="e-float-line"></span>
                @Html.Label("CustomerID", "Customer ID", new { @class = "e-float-text e-label-top" })
            </div>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group col-md-6">
            @Html.EJS().NumericTextBox("Freight").Format("C2").Placeholder("Freight").FloatLabelType(Syncfusion.EJ2.Inputs.FloatLabelType.Always).Render()
        </div>
        <div class="form-group col-md-6">
            @Html.EJS().DatePicker("OrderDate").Placeholder("Order Date").FloatLabelType(Syncfusion.EJ2.Inputs.FloatLabelType.Always).Render()
        </div>
    </div>

    <div class="form-row">
        <div class="form-group col-md-6">
            @Html.EJS().DropDownList("ShipCountry").Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "ShipCountry", Value = "ShipCountry" }).Placeholder("Ship Country").FloatLabelType(Syncfusion.EJ2.Inputs.FloatLabelType.Always).DataSource((IEnumerable<object>)@ViewBag.datasource).Render()
        </div>
        <div class="form-group col-md-6">
            @Html.EJS().DropDownList("ShipCity").Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "ShipCity", Value = "ShipCity" }).Placeholder("Ship City").FloatLabelType(Syncfusion.EJ2.Inputs.FloatLabelType.Always).DataSource((IEnumerable<object>)@ViewBag.datasource).Render()
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-12">
            <div class="e-float-input e-control-wrapper">
                @Html.TextArea("ShipAddress")
                <span class="e-float-line"></span>
                @Html.Label("ShipAddress", "Ship Address", new { @class = "e-float-text e-label-top" })
            </div>
        </div>
    </div>
</div>

@Html.EJS().ScriptManager()
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
} 

public ActionResult Editpartial(DialogTemplateModel value)
{
    var order = OrdersDetails.GetAllRecords();
    ViewBag.datasource = order;
    return PartialView("_DialogEditpartial", value);
}

public ActionResult AddPartial()
{
    var order = OrdersDetails.GetAllRecords();
    ViewBag.datasource = order;
    return PartialView("_DialogAddpartial");
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace EJ2MVCSampleBrowser.Models
{
    public class DialogTemplateModel
    {
        [Required]
        public int? OrderID { get; set; }
        [Required, MinLength(3)]
        public string CustomerID { get; set; }
        public int? EmployeeID { get; set; }
        public double Freight { get; set; }
        public string ShipCity { get; set; }
        public bool Verified { get; set; }
        public DateTime OrderDate { get; set; }

        public string ShipName { get; set; }

        public string ShipCountry { get; set; }

        public DateTime ShippedDate { get; set; }
        public string ShipAddress { get; set; }
    }
}
@model EJ2MVCSampleBrowser.Models.DialogTemplateModel
@*//define the model for store the model values*@

@using Syncfusion.EJ2

<div>
    <div class="form-row">
        <div class="form-group col-md-6">
            <div class="e-float-input e-control-wrapper">
                @Html.TextBox("OrderID", Model.OrderID.ToString(), new { disabled= true})
                <span class="e-float-line"></span>
                @Html.Label("OrderID", "Order ID", new { @class = "e-float-text e-label-top" })
            </div>
        </div>
        <div class="form-group col-md-6">
            <div class="e-float-input e-control-wrapper">
                @Html.TextBox("CustomerID", Model.CustomerID.ToString())
                <span class="e-float-line"></span>
                @Html.Label("CustomerID", "Customer Name", new { @class = "e-float-text e-label-top" })
            </div>
        </div>
    </div>

    <div class="form-row">
        <div class="form-group col-md-6">
            @Html.EJS().NumericTextBox("Freight").Value(Model.Freight).Format("C2").Placeholder("Freight").FloatLabelType(Syncfusion.EJ2.Inputs.FloatLabelType.Always).Render()
        </div>
        <div class="form-group col-md-6">
            @Html.EJS().DatePicker("OrderDate").Value(Model.OrderDate).Placeholder("Order Date").FloatLabelType(Syncfusion.EJ2.Inputs.FloatLabelType.Always).Render()
        </div>
    </div>

    <div class="form-row">
        <div class="form-group col-md-6">
            @Html.EJS().DropDownList("ShipCountry").Value(Model.ShipCountry).Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "ShipCountry", Value = "ShipCountry" }).Placeholder("Ship Country").FloatLabelType(Syncfusion.EJ2.Inputs.FloatLabelType.Always).DataSource((IEnumerable<object>)@ViewBag.datasource).Render()
        </div>
        <div class="form-group col-md-6">
            @Html.EJS().DropDownList("ShipCity").Value(Model.ShipCity).Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "ShipCity", Value = "ShipCity" }).Placeholder("Ship City").FloatLabelType(Syncfusion.EJ2.Inputs.FloatLabelType.Always).DataSource((IEnumerable<object>)@ViewBag.datasource).Render()
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-12">
            <div class="e-float-input e-control-wrapper">
                @Html.TextArea("ShipAddress", Model.ShipAddress)
                <span class="e-float-line"></span>
                @Html.Label("ShipAddress", "Ship Address", new { @class = "e-float-text e-label-top" })
            </div>
        </div>
    </div>
</div>

@Html.EJS().ScriptManager()

The Dialog/Inline template form editors should have name attribute.

Reading Values From Editors

You can read, format and update the current editor value in the ActionBeginevent at the time of requestType as save.

In the below code example, we have format and updated the freight value.

    function actionBegin(args) {
        if (args.requestType === 'save') {
            // cast string to integer value.
            args.data['Freight'] = parseFloat(args.form.querySelector("#Freight").value);
        }
    }

Set focus to editor

By default, the first input element in the dialog will be focused while opening the dialog. If the first input element is in disabled or hidden state, focus the valid input element in the ActionComplete event based on requestType as beginEdit.

    function actionComplete(args) {
        // Set initail Focus
        if (args.requestType === 'beginEdit') {
            (args.form.elements.namedItem('CustomerID')).focus();
        }
    }

Adding validation rules for custom editors

If you have used additional fields that are not present in the column model, then add the validation rules to the ActionComplete event.

    function actionComplete(args) => {
        if ((args.requestType === 'beginEdit' || args.requestType === 'add')) {
            // Add Validation Rules
            args.form.ej2_instances[0].addRules('Freight', {max: 500});
        }
    }

Adding a new row at the Bottom of the Grid

By default, a new row will be added at the top of the grid. You can change it by setting editSettings.newRowPosition as Bottom.

tagHelper
rowposition.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" newRowPosition="true"></e-grid-editSettings>
            <e-grid-columns>
                <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
                <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
                <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
                <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
            </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Add newRowPostion is supported for Normal and Batch editing modes.

Command column

The command column provides an option to add CRUD action buttons in a column. This can be defined by the Column.Commands property.

The available built-in command buttons are:

Command Button Actions
Edit Edit the current row.
Delete Delete the current row.
Save Update the edited row.
Cancel Cancel the edited state.
tagHelper
commandcolumn.cs
@{

        List<object> commands = new List<object>();
        commands.Add(new { type = "Edit", buttonOption = new { iconCss = "e-icons e-edit", cssClass = "e-flat" } });
        commands.Add(new { type = "Delete", buttonOption = new { iconCss = "e-icons e-delete", cssClass = "e-flat" } });
        commands.Add(new { type = "Save", buttonOption = new { iconCss = "e-icons e-update", cssClass = "e-flat" } });
        commands.Add(new { type = "Cancel", buttonOption = new { iconCss = "e-icons e-cancel-icon", cssClass = "e-flat" } });

    }
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="310">
            <e-grid-editSettings allowDeleting="true" allowEditing="true" mode="Normal"></e-grid-editSettings>
            <e-grid-columns>
                <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
                <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
                <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>
                <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>   
                <e-grid-column headerText="Manage Records" width="120" command=commands>
                </e-grid-column>
     
            </e-grid-columns>
</ejs-grid>
public IActionResult Index()
        {
            var orders= OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Custom command

The custom command buttons can be added in a column by using the Column.Commands property and the action for the custom buttons can be defined in the ButtonOption.Click event.

tagHelper
customcommand.cs
@{

    List<object> commands = new List<object>();
    commands.Add(new { type = "userstatus", buttonOption = new { content = "Details", cssClass = "e-flat e-details" } }); // custom
}

<div class="control-section">
    <ejs-grid id="Grid" dataSource="ViewBag.dataSource" load="load" allowPaging="true">
        <e-grid-editSettings allowAdding="false" allowDeleting="false" allowEditing="true"></e-grid-editSettings>
        <e-grid-columns>
            <e-grid-column field="OrderID" headerText="Order ID" validationRules="@(new { required=true})" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>
            <e-grid-column field="Verified" headerText="Verified" editType="booleanedit" displayAsCheckBox="true" textAlign="Center" type="boolean" width="100"></e-grid-column>
            <e-grid-column headerText="Manage Records" width="150" commands="commands"></e-grid-column>
        </e-grid-columns>
    </ejs-grid>
</div>
<script>
    function load() {
        this.columns[2].commands[0].buttonOption.click = function (args) {     //click event for custom command button
                var grid = document.getElementsByClassName('e-grid')[0].ej2_instances[0];  // grid instance
                var rowObj= grid.getRowObjectFromUID(ej.base.closest(args.target, '.e-row').getAttribute('data-uid'));
                alert(JSON.stringify(rowObj.data));  // display row data 
        }
    }
</script>
public IActionResult Index()
        {
            var orders = OrdersDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Confirmation messages

Delete confirmation

The delete confirm dialog can be shown when deleting a record by defining the ShowDeleteConfirmDialog as true

tagHelper
deleteconfirm.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update","Cancel"  })">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>          
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>            
 </ejs-grid>
public IActionResult Index()
        {
            var orders =OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

The ShowDeleteConfirmDialog supports all type of edit modes.

Batch confirmation

By default, grid will show the confirm dialog when saving or cancelling or performing any actions like filtering, sorting, etc.

tagHelper
batchconfirm.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update", "Cancel"  })">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Batch" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
 </ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Column validation

Column validation allows you to validate the edited or added row data and it display errors for invalid fields before saving data. Grid uses Form Validator component for column validation. You can set validation rules by defining the Columns.ValidationRules.

tagHelper
columnvalid.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"  validationRules="@(new { required= true })"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120" validationRules="@(new { required= true, minLength= 3 })"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
 </ejs-grid>
public IActionResult Index()
        {
            var orders = OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Persisting data in server

Edited data can be persisted in the database using the RESTful web services.

All the CRUD operations in the grid are done through DataManager. The DataManager has an option to bind all the CRUD related data in server-side.

For your information, the ODataAdaptor persists data in the server as per OData protocol.

In the below section, we have explained how to get the edited data details on the server-side using the UrlAdaptor.

URL adaptor

You can use the UrlAdaptor of DataManager when binding data source from remote data. In the initial load of grid, data are fetched from remote data and bound to the grid using url property of DataManager. You can map The CRUD operation in grid can be mapped to server-side Controller actions using the properties InsertUrl, RemoveUrl, UpdateUrl, CrudUrl and BatchUrl.

The following code example describes the above behavior.

tagHelper
urladaptor.cs
<ejs-grid id="Grid"  height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
    <e-datamanager url="/Home/UrlDataSource" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Delete" adaptor="UrlAdaptor"></e-datamanager>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public IActionResult UrlDatasource([FromBody]Data dm)
        {
            var order = OrdersDetails.GetAllRecords();            
            var Data = order.ToList();
            int count = order.Count();
            return dm.requiresCounts ? Json(new { result = Data.Skip(dm.skip).Take(dm.take), count = count }) : Json(Data);
        }

Also, when using the UrlAdaptor, you need to return the data as JSON from the controller action and the JSON object must contain a property as result with dataSource as its value and one more property count with the dataSource total records count as its value.

The following code example describes the above behavior.

tagHelper
data.cs
<ejs-grid id="Grid"  height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
    <e-datamanager url="/Home/UrlDataSource" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Delete" adaptor="UrlAdaptor"></e-datamanager>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public IActionResult UrlDatasource([FromBody]Data dm)
{
    if (order.Count() == 0)
        BindDataSource();
    var DataSource = order.ToList();
    int count = order.Count();
    DataResult result = new DataResult();
    result.result = DataSource.Skip(dm.skip).Take(dm.take).ToList();
    result.count = count;
    return dm.requiresCounts ? Json(new { result = Data.Skip(dm.skip).Take(dm.take), count = count }) : Json(Data);
}

public class DataResult
{
    public List<EditableOrder> result { get; set; }
    public int count { get; set; }
}

Insert record

Using the InsertUrl property, you can specify the controller action mapping URL to perform insert operation on the server-side.

The following code example describes the above behavior.

tagHelper
insert.cs
<ejs-grid id="Grid"  height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
    <e-datamanager url="/Home/UrlDataSource" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Delete" adaptor="UrlAdaptor"></e-datamanager>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public ActionResult Insert([FromBody]EditableOrder value)
{
    //Insert record in database
}

The newly added record details are bound to the value parameter. Please refer to the following screenshot.

insert

Update record

Using the UpdateUrl property, the controller action mapping URL can be specified to perform save/update operation on the server-side.

The following code example describes the previous behavior.

tagHelper
update.cs
<ejs-grid id="Grid"  height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
    <e-datamanager url="/Home/UrlDataSource" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Delete" adaptor="UrlAdaptor"></e-datamanager>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public ActionResult Update([FromBody]EditableOrder value)
{
    //Update record in database
}

The updated record details are bound to the value parameter. Please refer to the following screenshot.

update

Delete record

Using the RemoveUrl property, the controller action mapping URL can be specified to perform delete operation on the server-side.

The following code example describes the previous behavior.

tagHelper
delete.cs
<ejs-grid id="Grid"  height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
    <e-datamanager url="/Home/UrlDataSource" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Delete" adaptor="UrlAdaptor"></e-datamanager>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public ActionResult Delete([FromBody]int key)
{
    //Delete record in database
}

The deleted record primary key value is bound to the key parameter. Please refer to the following screenshot.

delete

CRUD URL

Using the CrudUrl property, the controller action mapping URL can be specified to perform all the CRUD operation at server-side using a single method instead of specifying separate controller action method for CRUD (insert, update and delete) operations.

The action parameter of CrudUrl is used to get the corresponding CRUD action.

The following code example describes the above behavior.

tagHelper
crudurl.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update", "Cancel"})">
    <e-datamanager url="/Home/DataSource" crudUrl="/Home/CrudUpdate"  adaptor="UrlAdaptor"></e-datamanager>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public IActionResult UrlDatasource([FromBody]Data dm)
{
    if (order.Count() == 0)
        BindDataSource();
    var DataSource = order.ToList();
    int count = order.Count();
    DataResult result = new DataResult();
    result.result = DataSource.Skip(dm.skip).Take(dm.take).ToList();
    result.count = count;
    return dm.requiresCounts ? Json(new { result = Data.Skip(dm.skip).Take(dm.take), count = count }) : Json(Data);
}

public class DataResult
{
    public List<EditableOrder> result { get; set; }
    public int count { get; set; }
}

        public ActionResult CrudUpdate([FromBody]EditableOrder value, string action)
        {
            if(action == "update"){
                //Update record in database
            }
            else if (action == "insert")
            {
                //Insert record in database
            }
            else
            {
                //Delete record in database
            }
        }

Please refer to the following screenshot to know about the action parameter.

crudupdate

If you specify InsertUrl along with CrudUrl, then while adding InsertUrl only will be invoked.

Batch URL

The BatchUrl property supports only for batch editing mode. You can specify the controller action mapping URL to perform batch operation on the server-side.

The following code example describes the above behavior.

tagHelper
batchurl.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update", "Cancel"})">
    <e-datamanager url="/Home/UrlDataSource" batchUrl="/Home/BatchUpdate"  adaptor="UrlAdaptor"></e-datamanager>
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal" showConfirmDialog="true" showDeleteConfirmDialog="true"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public IActionResult UrlDatasource([FromBody]Data dm)
        {
            var order = OrdersDetails.GetAllRecords();            
            var Data = order.ToList();
            int count = order.Count();
            return dm.requiresCounts ? Json(new { result = Data.Skip(dm.skip).Take(dm.take), count = count }) : Json(Data);
        }
public ActionResult BatchUpdate([FromBody]string action, List<EditableOrder> added, List<EditableOrder> changed, List<EditableOrder> deleted, int? key)
{
//Save the batch changes in database
}

batch

Default column values on add new

The grid provides an option to set the default value for the columns when adding a new record in it. To set a default value for the particular column by defining the Columns.DefaultValue.

tagHelper
defaultcolumnvalue.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update", "Cancel"})">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID"  defaultValue="HANAR" type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
        {
            var orders= OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Custom validation

You can define your own custom validation rules for the specific columns by using Form Validator custom rules.

In the below demo, custom validation applied for CustomerID column.

tagHelper
customvalidation.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })">
            <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal"></e-grid-editSettings>
            <e-grid-columns>
                <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100" validationRules="@(new{ required: true })"></e-grid-column>                
                <e-grid-column field="CustomerID" headerText="Customer ID"   type="string" width="120" validationRules="@(new { required: true, minLength: [customFn, 'Need atleast 5 letters'] })></e-grid-column>                
                <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>
                <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
            </e-grid-columns>
</ejs-grid>

<script>

function customFn (args) {
    return args['value'].length >= 5;
};

</script>
public IActionResult Index()
        {
            if (orders.Count() == 0)
                DataSource();
            ViewBag.datasource = orders;            
            return View();
        }

        public void DataSource()
        {
            int code = 10000;
            for (int i = 1; i < 10; i++)
            {
                orders.Add(new OrderDetails(code + 1, "ALFKI", i + 0, 2.3 * i, false, new DateTime(1991, 05, 15), "Berlin", "Simons bistro", "Denmark", new DateTime(1996, 7, 16), "Kirchgasse 6"));
                orders.Add(new OrderDetails(code + 2, "ANATR", i + 2, 3.3 * i, true, new DateTime(1990, 04, 04), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11), "Avda. Azteca 123"));
                orders.Add(new OrderDetails(code + 3, "ANTON", i + 1, 4.3 * i, true, new DateTime(1957, 11, 30), "Cholchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"));
                orders.Add(new OrderDetails(code + 4, "BLONP", i + 3, 5.3 * i, false, new DateTime(1930, 10, 22), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30), "Magazinweg 7"));
                orders.Add(new OrderDetails(code + 5, "BOLID", i + 4, 6.3 * i, true, new DateTime(1953, 02, 18), "Tsawassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3), "1029 - 12th Ave. S."));
                code += 5;
            }
        }

Disable editing for particular column

You can disable editing for particular columns by using the Columns.AllowEditing.

In the following demo, editing is disabled for the CustomerID column.

tagHelper
disableeditforcolumn.cs
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" height="273" toolbar="@(new List<string>() { "Add", "Edit", "Delete","Update", "Cancel" })">
    <e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true" mode="Normal"></e-grid-editSettings>
    <e-grid-columns>
        <e-grid-column field="OrderID"  headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>                
        <e-grid-column field="CustomerID" headerText="Customer ID"   allowEditing="false"  type="string" width="120"></e-grid-column>                
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" editType="numericedit" width="120"></e-grid-column>                               
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>                
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
        {
            var orders = OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Troubleshoot: Editing works only for first row

The Editing functionalities can be performed based upon the primary key value of the selected row. If primaryKey is not defined in the grid, then edit or delete action take places the first row.

See also