New to Telerik UI for ASP.NET CoreStart a free 30-day trial

Creating a Dynamic Grid with Batch Editing

Environment

ProductGrid for Progress® Telerik® UI for ASP.NET Core

Description

How can I create a dynamic Telerik UI Grid in ASP.NET Core that uses batch editing?

Solution

The suggested approach demonstrates how to bind a DataTable to a Telerik UI Grid and enable the Batch editing.

  1. Populate the Grid columns based on the available Model.Columns. Model.Columns is the collection of columns of the created DataTable.
  2. Set the column title by using the Caption property of the DataColumn.
  3. Activate the Batch editing: .DataSource(dataSource => dataSource.Ajax().Batch(true)).
  4. Define the Model in the DataSource configuration by using the ColumnName and DataType properties of the DataColumns. Also, it is important to set the Model Id to match the DataTable PrimaryKey.
  5. On the controller side, obtain each of the modified DataTable records by using the [Bind(Prefix = "models")] attribute.
Index.cshtml
    @model System.Data.DataTable

    @(Html.Kendo().Grid<dynamic>()
        .Name("Grid")
        .Columns(columns =>
        {
            foreach (System.Data.DataColumn dcolumn in Model.Columns)
            {
                switch (dcolumn.DataType.ToString())
                {
                    case "System.Int16":
                    case "System.Int32":
                    case "System.Int64":
                        columns.Bound(dcolumn.ColumnName).Title(dcolumn.Caption).EditorTemplateName("Integer");
                        break;

                    case "System.Decimal":
                    case "System.Double":
                    case "System.Float":
                        columns.Bound(dcolumn.ColumnName).Title(dcolumn.Caption).EditorTemplateName("Number");
                        break;
                    case "System.String":
                        columns.Bound(dcolumn.ColumnName).Title(dcolumn.Caption).EditorTemplateName("String");
                        break;
                    case "System.Byte":
                    case "System.Boolean":
                        columns.Bound(dcolumn.ColumnName).Title(dcolumn.Caption).EditorTemplateName("Boolean");
                        break;
                    case "System.DateTime":
                        columns.Bound(dcolumn.ColumnName).Title(dcolumn.Caption).Format("{0:d}").EditorTemplateName("Date");
                        break;
                    default:
                        columns.Bound(dcolumn.ColumnName).Title(dcolumn.Caption).EditorTemplateName("String");
                        break;

                }
            }

            columns.Command(command => command.Destroy());
        })
        .ToolBar(toolbar => {
            toolbar.Create();
            toolbar.Save();
        })
        .Pageable()
        .Sortable()
        .Editable(editable => editable.Mode(GridEditMode.InCell))
        .Filterable()
        .Groupable()
            .DataSource(dataSource => dataSource
                .Ajax()
                .Batch(true)
                .Model(model =>
                {
                    var id = Model.PrimaryKey[0].ColumnName;
                    model.Id(id);
                    foreach (System.Data.DataColumn column in Model.Columns)
                    {
                        var field = model.Field(column.ColumnName, column.DataType);
                        if (column.ColumnName == id) {
                            field.Editable(false);
                        }

                    }
                })
                .Create(create => create.Action("Customers_Create", "DynamicBatchEditing"))
                .Read(read => read.Action("Customers_Read", "DynamicBatchEditing"))
                .Update(update => update.Action("Customers_Update", "DynamicBatchEditing"))
                .Destroy(destroy => destroy.Action("Customers_Destroy", "DynamicBatchEditing"))
        )
    )

For a complete and autonomous example of the aforementioned approach, refer to the following GitHub example.

More ASP.NET Core Grid Resources

See Also