0

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);
Harry
  • 41
  • 1
  • 2
  • 5

2 Answers2

1

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; }
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • sorry, im bit new to mvc3 so im not able to get it right wat ur saying, can you be little bit more clear for my better understanding...what is viewmodel, do u want me to create a new view or model? currently in my view im using this @model WebgridPaging.Models.User – Harry Apr 24 '13 at 09:06
  • A view model is a class that you specifically define for each of your views. In this class you include only the properties that the view will require. And then you have your controller action pass this view model to the view. You mentioned that in your view you are using `@model WebgridPaging.Models.User`, but then I guess that you are binding the WebGrid to some property of this User class because the grid must be bound to an `IEnumerable`. So in your case you could have a `UserViewModel` which will contain an `IEnumerable` property to bind the grid to. – Darin Dimitrov Apr 24 '13 at 09:29
  • i have edited my question, have a look at it...as per your suggestion i created a new model and gave a property public int RowNumber { get; set; }, how should i proceed from here? – Harry Apr 25 '13 at 04:26
0

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)