December 08 2013

ASP.net MVC 4 Razor WebGrid auto increase row number

Tagged Under :

asp.net
Below example showing you how to add the row number in WebGrid.

You just need add the following script inside the grid columns.
grid.Column(header: "No.", 
                format: item => item.WebGrid.Rows.IndexOf(item) + 1 + Math.Round(Convert.ToDouble(grid.TotalRowCount / grid.PageCount) / grid.RowsPerPage) * grid.RowsPerPage * grid.PageIndex)

The completed WebGrip example as below:
@{
    var grid = new WebGrid(@Model, 
    @grid.GetHtml(
    columns: new [] {
        grid.Column(header: "No.", 
                    format: item => item.WebGrid.Rows.IndexOf(item) + 1 + Math.Round(Convert.ToDouble(grid.TotalRowCount / grid.PageCount) / grid.RowsPerPage) * grid.RowsPerPage * grid.PageIndex),
        grid.Column("Title"),
        grid.Column("Date")
    }
}

Make a Comment

You must be logged in to post a comment.