asp.net-mvcmvcgrid

adding row index to gridmvc in asp.net mvc


I am trying to add rowindex auto to grid.

My code is:

columns.Add().
        Titled("row").
        SetWidth(20).
        Sortable(true).
        Filterable(true).
        RenderValueAs(i++.ToString());

columns.Add(m => m.Hotel_Name_Fa).
        Titled("name").
        SetWidth(50).
        Sortable(true).
        Filterable(true);

I want this result:

 row                name
  1                  name
  2                  name2  

Solution

  • define these in the view at the top

       int rowNumber = 0;
       int pageSize = 5;
       int pageNumber = Convert.ToInt32(ViewBag.PageNumber);
       if ((pageNumber) > 1)
       {
           rowNumber = (pageSize * (pageNumber-1)) + rowNumber;
       }
    

    and then in the grid

    columns.Add()
            .Titled("#")
            .SetWidth(10)
            .RenderValueAs(o =>
              {
                  rowNumber++;
                  return @<text> @rowNumber </text>;
              });
    

    in your controller

            ViewBag.PageNumber = (Request.QueryString["grid-page"] == null) ? "1" : Request.QueryString["grid-page"];