C#中datagridview前添加序号

首先,双击datagridview事件中的RowPostPaint添加一个事件函数体,

然后,在函数体中添加如下代码:

SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);

运行即可。。。。。

原文地址:https://www.cnblogs.com/jamse/p/datagridview.html