New to Telerik UI for ASP.NET MVC? Start a free 30-day trial
Creating a Dynamic Grid with Batch Editing
Environment
Product | Grid 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.
- Populate the Grid columns based on the available
Model.Columns
.Model.Columns
is the collection of columns of the createdDataTable
. - Set the column title by using the
Caption
property of theDataColumn
. - Activate the Batch editing:
.DataSource(dataSource => dataSource.Ajax().Batch(true))
. - Define the
Model
in theDataSource
configuration by using theColumnName
andDataType
properties of theDataColumns
. Also, it is important to set the ModelId
to match theDataTable
PrimaryKey
. - 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.