I've searched and found a variety of answers regarding binding to dynamic data. The closest I've come is using a DataTable but I can't format the data specifically using it (i.e. currency, date). Basically we want to allow our users to see their 'User Defined FIelds' in the grid, in addition to the regular items we have in a grid. I planned on converting all columns (user defined and our standard columns we have for all customers) into one column object:
public class GenericGridListItemViewModel
{
public string Name { get; set; } = null!;
public string? TextValue { get; set; }
public double? NumberValue { get; set; }
public DateTime? DateValue { get; set; }
public decimal? CurrencyValue { get; set; }
public bool Hidden { get; set; }
public UdfVariableTypes VariableType { get; set; }
}
And then in then I'd iterate through them an use the variable type for formatting the column and the hidden to define if it's hidden. The below example does not include all the possible variable types but it shows my attempt at getting something up and running. I get the correct headers on the grid but no rows are filled
The data is returned from the controller as a List<List<GenericGridListItemViewModel>>
If anyone has some insight I'd be thrilled to figure this out. Thanks, Brian.
@(
Html.Kendo().Grid <List<GenericGridListItemViewModel>>()
.Name("Grid")
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Contacts_Read", "Contacts", new { area = "Contacts" }))
.PageSize(1000)
.ServerOperation(false)
)
.Columns(columns =>
{
int cdx = 0;
foreach(var col in Model.Data.First())
{
if (col.VariableType == UdfVariableTypes.Integer)
{
columns.Bound(l => l[cdx].NumberValue).Title(col.Name);
}
else if (col.VariableType == UdfVariableTypes.Text)
{
columns.Bound(l => l[cdx].TextValue).Title(col.Name);
}
else if (col.VariableType == UdfVariableTypes.Currency)
{
columns.Bound(l => l[cdx].NumberValue).Title(col.Name).Format("{0:C}").Hidden(Model.Data.First()[cdx].Hidden);
}
cdx++;
}
})
.Pageable()
.Sortable()
.Filterable()
.ColumnMenu()
.ToolBar(t =>
{
t.Excel();
t.Search();
})
.Reorderable(l => l.Columns(true))
)