1、设置 RowPostPaint 为true
2、启用RowPostPaint事件
/// <summary>
/// DataGridView显示行号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridWebConfigView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
string strRowNumber = (e.RowIndex + 1).ToString();
while (strRowNumber.Length < dataGridWebConfigView.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
if (dataGridWebConfigView.RowHeadersWidth < (int)(size.Width + 20)) dataGridWebConfigView.RowHeadersWidth = (int)(size.Width + 20);
Brush b = SystemBrushes.ControlText;
e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
}