Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8404595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:38:18+00:00 2026-06-09T22:38:18+00:00

I want to have a column as row number in MVC WebGrid. How can

  • 0

I want to have a column as row number in MVC WebGrid. How can I do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:38:20+00:00Added an answer on June 9, 2026 at 10:38 pm

    You could use a view model that will contain a property indicating the row number.

    Let’s suppose that you have the following domain model:

    public class DomainModel
    {
        public string Foo { get; set; }
    }
    

    Now you build a view model that will correspond to the requirements of your view:

    public class MyViewModel
    {
        public int RowNumber { get; set; }
        public string Foo { get; set; }
    }
    

    and then:

    public ActionResult Index()
    {
        // fetch the domain model from somewhere
        var domain = Enumerable.Range(1, 5).Select(x => new DomainModel
        {
            Foo = "foo " + x
        });
    
        // now build the view model
        // TODO: use AutoMapper to perform this mapping
        var model = domain.Select((element, index) => new MyViewModel
        {
            RowNumber = index + 1,
            Foo = element.Foo
        });
    
        return View(model);
    }
    

    Now your view becomes strongly typed to the view model of course:

    @model IEnumerable<MyViewModel>
    
    @{
        var grid = new WebGrid(Model);
    }
    
    @grid.GetHtml(
        columns: grid.Columns(
            grid.Column("RowNumber"),
            grid.Column("Foo")
        )
    )
    

    Now let’s suppose that for some foolish reason you don’t want to use view models. In this case you could turn your view into spaghetti code if you prefer:

    @model IEnumerable<DomainModel>
    
    @{
        var grid = new WebGrid(Model.Select((element, index) => new { element, index }));
    }
    
    @grid.GetHtml(
        columns: grid.Columns(
            grid.Column("RowNumber", format: item => item.index + 1),
            grid.Column("Foo", format: item => item.element.Foo)
        )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one column matrix with n rows. I want difference between row 2
I have a grid column, of which, row X is hidden. I want to
I have a GridView with a row number (Container.DataItemIndex) column in ASP.NET (1,2,3,4, ...)
I have an ID column in a table which stores the row ID number
I have a list view with 10 columns and I want each row to
I have a csv file with 5 columns. I want to fetch the row
a have three column layout and i want to have three divs inside the
I really want to have a column of inifinite length for one of the
I want to have a 2 column layout, and have the left column able
I have a column of Int Array. I want to add another column to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.