Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Auto Generate Row Number [solved]

Auto Generate Row Number [solved]

Scheduled Pinned Locked Moved C#
helptutorialquestion
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Elango N
    wrote on last edited by
    #1

    How to populate row number for datagridview in row header column with out any looping statements? it will be very usful for me if you help.

    Elango.N

    modified on Tuesday, June 15, 2010 5:51 AM

    L 1 Reply Last reply
    0
    • E Elango N

      How to populate row number for datagridview in row header column with out any looping statements? it will be very usful for me if you help.

      Elango.N

      modified on Tuesday, June 15, 2010 5:51 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can implement your custom datagridview by overriding it and then use it's OnRowPostPaint method to draw row number. That's it. No whenever you use this grid it will automatically add row number to the datagridview. This is the simplest solution I have to apply row number in datagridview without any kind of looping.

      /// 
      /// This class extends the the DataGridView so row numbers will 
      /// automatically appear in the row header cells. In this 
      /// implementation, the width of the column that contains the row 
      /// header cells is automatically adjusted to accomodate the row 
      /// numbering.
      /// 
      class MyDGV : DataGridView
      {
          public MyDGV()
          {
              //perform any necessary customization initialization here
      
          } //end default constructor
      
          protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
          { 
            //this method overrides the DataGridView's RowPostPaint event 
            //in order to automatically draw numbers on the row header cells
            //and to automatically adjust the width of the column containing
            //the row header cells so that it can accommodate the new row
            //numbers,
      
              //store a string representation of the row number in 'strRowNumber'
              string strRowNumber = (e.RowIndex + 1).ToString();
              
              //prepend leading zeros to the string if necessary to improve
              //appearance. For example, if there are ten rows in the grid,
              //row seven will be numbered as "07" instead of "7". Similarly, if 
              //there are 100 rows in the grid, row seven will be numbered as "007".
              while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
              
              //determine the display size of the row number string using
              //the DataGridView's current font.
              SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
              
              //adjust the width of the column that contains the row header cells 
              //if necessary
              if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);
              
              //this brush will be used to draw the row number string on the
              //row header cell using the system's current ControlText color
              Brush b = SystemBrushes.ControlText;
      
              //draw the row numbe
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups