Winform 程序中DataGridView 控件添加行号

  private void dataGridViewInventory_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
           var dgv = sender as DataGridView;
            if(dgv != null)
            {
                System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth, e.RowBounds.Height);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            } }
原文地址:https://www.cnblogs.com/xakml/p/RowNo.html