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 7574035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:20:01+00:00 2026-05-30T16:20:01+00:00

There are many questions concerning this issue here, but I have tried the solutions

  • 0

There are many questions concerning this issue here, but I have tried the solutions posted and still don’t get it to work.

I have a datagridview where I want to show the rownumber on the row headers.
This is what I have tried:

 gridView.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;           
            gridView.AutoResizeRowHeadersWidth(
                DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);


            foreach (DataGridViewRow row in gridView.Rows)
            {
                row.HeaderCell.Value = (row.Index + 1).ToString();
            }

This code is called from the OnLoad event, since in other questions it is stated that the code should not be run in the constructor.

Advice? Thanks!

  • 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-05-30T16:20:03+00:00Added an answer on May 30, 2026 at 4:20 pm

    I think my approach would be to bind a row on the ItemDataBound event rather than going over the rows all a single place. Something along the lines of:

        /// <summary>
        /// Which row is currently being rendered
        /// </summary>
        protected int RowIndex { get; set; }
    
        protected override void OnLoad(EventArgs e)
        {      
          this.RowIndex = 0;
          this.DataGrid.DataSource = new string[] { "a", "b", "c" }; // bind the contents
          this.DataGrid.DataBind();      
        }
    
        /// <summary>
        /// When an item is bound
        /// </summary>
        protected void OnItemDataBound(object sender, DataGridItemEventArgs e)
        {
          this.RowIndex++;
          Label label = e.Item.FindControl("RowLabel") as Label;
          if (label != null)
          {
            label.Text = this.RowIndex.ToString();
          }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are many questions on this, but I don't get it to
I know there are many questions concerning this topic but after reading them all
There are many questions like this but I can't find one that seems to
There are many questions like this, but none of them seem to answer my
NOTE: I know there are many questions that talked about that but I'm still
There are many questions relating to this error on Stack Overflow, and I've tried the
I know there are many questions like this already on SO but none of
There are many questions related to Stackless Python. But none answering this my question,
I know there are a couple questions on SO already concerning this but none
I know there are many questions similar but any of them didn't help. in

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.