Harry
Harry

Reputation: 41

implement row number in mvc3 webgrid along with paging?

i want to autogenerate rownumber for webgrid and im able to do it with the below code

grid.Column(header: "RowNumber", format: item => item.WebGrid.Rows.IndexOf(item) + 1),

the problem with this is that, im also implementing paging for my grid and when im clicking next page again the row numbers are being generated from sno 1, when my page 1 consists of records from row no 1 to 5, my page 2 row numbers should start from row no 6, but again my page 2 row numbers are starting from row no1..

im posting my flow of fetching the data and binding the data to grid

in the first step i have my model User.cs,which is interacting with database and fetching the data and storing the data in a List<>

in the next step, from my controller action method im invoking the method in my model User.cs which will fetch the data from the database and store it in the List<>

In the 3rd step, from my view im giving the source property of my webgrid to the list in my model which is holding the data

 var grid = new WebGrid(source: WebgridPaging.Models.User.Users, rowsPerPage: 2,canPage:true,canSort:false);

Upvotes: 0

Views: 2498

Answers (2)

user2496621
user2496621

Reputation: 11

Try the following

var cnt = Math.Round(Convert.ToDouble(grid.TotalRowCount / grid.PageCount) / grid.RowsPerPage) * grid.RowsPerPage * grid.PageIndex;

grid.Column(header: "S.NO.", format: item => item.WebGrid.Rows.IndexOf(item) + 1 + cnt)

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

I would recommend you using a view model (instead of passing your domain model to the view), i.e.:

@model IEnumerable<MyViewModel>

instead of:

@model IEnumerable<SomeDomainModelProbablyComingFromAnAutogeneratedEFContext>

Now in your view model you are free to have an RowNumber property:

public int RowNumber { get; set; }

Upvotes: 1

Related Questions