I'm currently working on a project, where I need to create a Grid with columns chosen by the user. These columns can originate from arbitrary tables in the database context. To make things worse the database is managed by a different software, and tables and table fields can change frequently. This will not change in the forseeable future.
So I can't just create a model, that contains all possible columns. Instead I'm using the dynamic
type to hand the data over to the view. This works quite good with every option I added to the Kendo Grid - except for making it editable. To make the example easier to understand I left out everything not relevant (like .Sortable()
or the method to read data via Ajax)
ColumnData[]
is an array of all columns displayed, containing their type and name
@model ColumnData[]
@(Html.Kendo().Grid<dynamic>()
.Name("grid")
.Columns(columns =>
{
for (var i = 0; i < Model.Length; i++)
{
columns.Bound(Model[i].Type, Model[i].Name);
}
})
//.Editable(editable => editable.Mode(GridEditMode.InLine))
.DataSource(dataSource => dataSource
.Ajax()
)
)
This works as intended until I uncomment the .Editable
part. Then it throws an exception:
System.InvalidOperationException: Templates can be used only with field access, property
access, single-dimension array index, or single-parameter custom indexer expressions
My guess is, that it uses a lamba function somewhere, which cannot be used with dynamic objects. I have no idea, how I could get around this though.
Since making the grid editable at runtime is not possible (see here) I think you should initialise the entire grid on the client side. You can find a good example example here.