0

I have Kendo MVC grid and the columns of the grid are dynamic based on the model that gets filled on the datasource read method. I need to loop through the dynamic columns to generate it on the grid.

I have kept autobind(false) and have called the read method on button click. But when the kendo grid columns gets generated the list on which i am looping through has 0 count as that gets filled after the read method. Columns generation is happening before the read method. I need to regenerate columns after the read method. grid.refresh() is not working for me.

.AutoBind(false)
                            .Columns(columns =>
                            {
                                columns.Bound(x => x.Name).Title("Name");
                                for (int i = 0; i < Model.obj.lstdateValueDicts.Count; i++)
                                {
                                    var index = i;
                                    columns.Template(@<text>@Convert.ToString(Model.obj.lstdateValueDicts[index])</text>).Title(Model.obj.lstdateValueDicts[i].Value);
                                }
                            })

Script:(on button click)

var grid = $('#FlowSheetsGrid').data('kendoGrid');
    grid.dataSource.read();
    debugger;
    grid.refresh();
    $("#FlowSheetSearchResults").show();

I expect the columns to get regenerated after the read method is over so the the list on which i am trying to generate the columns will get some count and it will get generated. Right now its getting 0 count and thus columns in loop does not get generated.

Shubhankar Bapat
  • 81
  • 1
  • 1
  • 8
  • First of all: Please show the entire grid definition. Secondly: The columns will not be regenerated if the datasource gets refreshed. I had a similar problem once. If I wanted to refresh the data (which might change the columns), I had to load them with a separate ajax call and redefine the entire grid based on the returned data. I guess that is what you have to do, too. – Carsten Franke Aug 12 '19 at 07:00

1 Answers1

0

Give this read function to the kendo grid and try

.DataSource(dataSource => dataSource .Ajax() .PageSize(20) .Read(read => read.Action("loadMydata","MyController")) )

Nijin P J
  • 1,302
  • 1
  • 9
  • 15