DataGridView加上默认的序号

DataGridView自动添加序号

DataGridView找到RowPostPaint事件

  private void dg_item_info_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SetDataGridViewIndex(this.dg_item_info, e);    
        }
        
     // 设置分页属性
private static int pageIndex1 = 1; private static int pageSize1 = 10; /// <summary> /// 给GridView 添加默认序号 /// </summary> public static void SetDataGridViewIndex(DataGridView dgv, DataGridViewRowPostPaintEventArgs e) { Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1 + ((pageIndex1 - 1) * pageSize1)).ToString(), dgv.RowHeadersDefaultCellStyle.Font, rectangle, dgv.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); }
原文地址:https://www.cnblogs.com/besos/p/13328912.html