Articles in this section
Category / Section

Cell editing using Multiline Textbox Control in ASP.NET MVC Grid.

3 mins read

By default, the TextBox control will be used as a default editor control for the string column in ASP.NET MVC Grid. You can customize the editor control using Multiline Textbox by rendering it as a custom component in the Grid edit form.

You can use the cellEditTemplate feature of Grid to render the Multiline TextBox component in the Grid edit form. The cell edit template is used to add a custom component for a particular column by invoking the create, write, read, and destroy functions.

Using the valueAccessor feature of Grid, you can split the value into multiple lines in the Grid column.

Note:

When editing a particular cell in the Grid, you can prevent the ENTER key’s functionality using the created event in the Grid.

 

@{
    var valueAccessor = "valueAccessorFn";
}
 
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.datasource).Created("created").Columns(col =>
{
    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(30).Add();
    col.Field("Freight").HeaderText("Freight").Width(120).Add();
    col.Field("ShipCity").HeaderText("Ship City").Width(150).DisableHtmlEncode(false).ValueAccessor(valueAccessor).Edit(new { create = "create", read = "read", destroy = "destroy", write = "write" }).Add();
 
}).EditSettings(edit => edit.AllowEditing(true)).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel", "Search" }).Render()
 
 
<script>
    var textEditor;
    var elemContent;
 
    function create(args) {
        elemContent = document.createElement('textarea');
        return elemContent;
    }
 
    function write(args) {
        textEditor = new ej.inputs.TextBox({
            multiline: true,
            value: args.rowData[args.column.field],
            floatLabelType: 'Auto'
        });
        textEditor.appendTo(elemContent);
    }
 
    function destroy() {
        textEditor.destroy();
    }
 
    function read(args) {
        return textEditor.value;
    }
    function created(args) {
        document.getElementById('Grid').ej2_instances[0].keyConfigs.enter = '';
    }
    function valueAccessorFn(field, data, column) {
        var value = data[field];
        if (value !== undefined) {
            return value.split("\n").join("<br>");
        } else {
            return "";
       }
   }
</script>

 

Output

Before editing in ASP MVC Grid

 

Before editing in ASP MVC Grid

 

You can find the sample here:

ASP MVC Grid Sample

 

Documentation



Conclusion

I hope you enjoyed learning about Cell editing using Multiline Textbox Control in ASP.NET MVC Grid.

You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.  You can also explore our ASP.NET MVC Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied