0

I have the following code :

@(Html.Kendo().Grid<DataMatrixPrinter.ViewModels.Business.ResearcherVM.ResearcherModel>()
                            .Name("GridKendoResearcherGrid")
                            .DataSource(
                            datasource => datasource
                                .Ajax()
                                .PageSize(20)
                                .Read(read => read.Url("/api/ResearcherApi/GetResearchers").Data("AdvSearch"))
                                )
                                .Columns(columns =>
                                {
                                    columns.Bound(c => c.ID).Title("ID");
                                    columns.Bound(c => c.fullName).Title("Name");

                                })
                             .Pageable()
                             .Sortable()
                             .Selectable()
                             .Events(events => events.Change("UpdateRecordKendo")))

I want to display the number for each row. So I changed the following command, but the value of this column is empty.

    @{
   int counter = 1;
}

@(Html.Kendo().Grid<DataMatrixPrinter.ViewModels.Business.ResearcherVM.ResearcherModel>()
                                .Name("GridKendoResearcherGrid")
                                .DataSource(
                                datasource => datasource
                                    .Ajax()
                                    .PageSize(20)
                                    .Read(read => read.Url("/api/ResearcherApi/GetResearchers").Data("AdvSearch"))
                                    )
                                    .Columns(columns =>
                                    {
                                        columns.Template(@<text><span>@(counter++)</span></text>).Title("#");
                                        columns.Bound(c => c.fullName).Title("Name");

                                    })
                                 .Pageable()
                                 .Sortable()
                                 .Selectable()
                                 .Events(events => events.Change("UpdateRecordKendo")))

Why does not the value appear?

solmaz ahamdi
  • 150
  • 1
  • 11
  • Possible duplicate of [How to add row number to kendo ui grid?](https://stackoverflow.com/questions/17378361/how-to-add-row-number-to-kendo-ui-grid) – Carsten Franke Jul 08 '19 at 07:57
  • @CarstenFranke , The proposed method uses javascript. Is not the same! – solmaz ahamdi Jul 08 '19 at 08:37
  • 1
    You are right. But I like the idea of people trying to use a proposed solution and use it for their needs... All you have to do is to use a client template for your column and retrieve the current counter from a JavaScript variable. E.g. `ClientTemplate("#=getVersion()#")` The function `getVersion()` has to return the number. – Carsten Franke Jul 08 '19 at 08:48

1 Answers1

0

You cannot use columns.Template(@<text><span>@(counter++)</span></text>) because your grid is using client-binding. You have to use .ClientTemplate() method and JavaScript to get the row number as suggested by Carsten Franke in his comment.

Martin D.
  • 1,950
  • 3
  • 23
  • 33