I have a kendo ui grid in my page that has some columns. Now I want to add a column to it that shows me row number. How to I do this? Thanks.
-
Show your Razor markup pls – YD1m Jun 29 '13 at 08:50
-
1[This](http://stackoverflow.com/questions/21112330/how-can-i-have-row-number-in-kendo-ui-grid/34609105#34609105) answer is good even if you use paginated grid. – Morteza Tourani Jan 05 '16 at 10:18
8 Answers
Initialize a variable and show it in column as template: "#= ++record #"
Here is code:
var record = 0;
$("#grid").kendoGrid({
dataSource: {
data: [{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" },{ foo: "foo" }, { foo: "foo" }, { foo : "foo1" }],
pageSize: 10
},
sortable: true,
columns: [ {
title: " ",
template: "#= ++record #",
width: 30
}, { field: "foo" }],
pageable: true,
dataBinding: function() {
record = (this.dataSource.page() -1) * this.dataSource.pageSize();
}
});

- 28,160
- 11
- 74
- 110
-
5Note: The `record` variable should be defined as a global (window) variable. If this js code is being written in global scope, no problem. But if you are writing this code in a function, you should change the `var record=0` to `window.record=0`, or you'll get an exception. – Mohammad Dehghan Dec 10 '13 at 02:53
-
For Razor you also need to actually show the number also: (not enough point thingies to comment)
above the grid:
@{int counter = 1;}
inside column definitions:
columns.Template(@<text>
<span>@counter @{ counter++; }</span>
</text>).Title("#");

- 79
- 2
- 3
-
-
the same thing not working for me here is my code @{int counter = 1;} columns.Template(@
@counter @{ counter++; } ).Title("S.no"); – Razim Khan Apr 06 '16 at 07:40
there is no need to define any variables, you can get help from databound event:
$("#grid").kendoGrid({
sortable: true,
dataSource: [{
name: "Jane Doe",
age: 30
}, {
name: "John Doe",
age: 33
}],
columns: [{
field: "name"
}, {
field: "age"
}, {
field: "rowNumber",
title: "Row number",
template: "<span class='row-number'></span>"
}],
dataBound: function () {
var rows = this.items();
$(rows).each(function () {
var index = $(this).index() + 1
+ ($("#grid").data("kendoGrid").dataSource.pageSize() * ($("#grid").data("kendoGrid").dataSource.page() - 1));;
var rowLabel = $(this).find(".row-number");
$(rowLabel).html(index);
});
}
});

- 28,160
- 11
- 74
- 110

- 6,924
- 1
- 41
- 46
YD1m's template did not work for me it treated the variable like a string
. So I had to implement it like so:
columns.Template(@<text>@((long)counter++)</text>)

- 11,411
- 10
- 42
- 70

- 41
- 1
For asp.net mvc wrapper you should use something like this:
@{
var counter = 1;
}
@( Html.Kendo().Grid<Types>()
.Name("grid")
.Columns(columns =>
{
// Define a template column with row counter
columns.Template(@<text>@counter++</text>);
// Define a columns from your data set and set a column setting
columns.Bound(type => type.id);
columns.Bound(type=> type.name).Title("Name");
// add more columns here
})
)

- 5,845
- 2
- 19
- 23
-
1This code apparently correct. But not worked for me. When I use "Columns.Template(@
"EVERTYTHINGS" );", this is not anythings in grid. – Tavousi Jun 29 '13 at 10:08
Based on Ehsan Mirsaeedi's great answer, I came up with this version, which assigns indices starting at 0 instead of row numbers starting at 1, skips group headers if the grid is grouped, and handles the case when the grid is not paged.
I needed these indices in order to add a template with a hidden input to each column, so that I can then submit the grid's values along with the entire form.
I think it's related enough and worth adding to the thread...
Code:
var theGrid = $("#grid").data("kendoGrid");
var rows = this.items().filter(function (index, item) {
return $(item).hasClass("k-grouping-row") == false;
});
$(rows).each(function () {
var index = $(this).index();
//prevent group header rows from incrementing index
if (theGrid.dataSource.options.group != null && theGrid.dataSource.options.group.length > 0)
index -= $(this).prevAll("#grid tr.k-grouping-row").length;
//adjust indices for current page
if (theGrid.options.pageable == true)
index += theGrid.dataSource.pageSize() * (theGrid.dataSource.page() - 1);
//add the value to the grid's data object
theGrid.dataItem(this).rowNumber = index;
//and update the grid's HTML
var rowLabel = $(this).find(".row-number");
$(rowLabel).html(index);
});

- 301
- 1
- 6
If you need, row number in editable grid
$(document).ready(function(){
$("#grid").kendoGrid({
dataSource: {
data: null,
schema: {
model: {
id: "register_No",
fields: {
register_No: {editable: true},
myEditableField: {editable: true},
}
}
}
},
edit:function(e){
var fields= $('input[name="register_No"]');
fields.attr('disabled', true);
fields.removeClass('k-input k-textbox');
fields.addClass('none');
//create this class and set border and background none
$('input[name="myEditableField"]').focus();
},
save:function(e){
var total=e.sender.dataSource.data().length;
if(e.model.register_No==""){
e.model.register_No=total;
}
},
remove:function(e){
var grid = $("#grid").data("kendoGrid");
var todos=grid.dataSource.data();
var id_actual=e.model.register_No;
var count=1;
for(var i=0;i<todos.length;i++){
if(todos[i].register_No!==id_actual){
var data = grid.dataSource.at(i);
data.set("register_No", count);
count++;
}
}
}
, height: 280,
toolbar: ["create"],
scrollable: true,
editable: {mode:"inline", createAt: "bottom", confirmation: true
}
,
columns: [
{field: "register_No",title: "No", width: 30},
{field: "myEditableField", title: "Any Field", width: 120},
{field: "options", command: ["edit", "destroy"], width: 200}
]
});
});
<div id="grid"></div>

- 164
- 11
Declare a variable for row counting:
var rowNumber = 0;
Use it in your template with ++ operator to increment it for each iteration:
#= ++rowNumber #
This also works for Kendo UI ListView.
See the official document:
http://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/Templates/add-row-numbers

- 1,420
- 1
- 12
- 6