方法1:简单RowStateChanged事件
private void dgv_list_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
}
方法2:RowPostPaint事件,这个能设置样式、字体、宽度,看上去更好看一些
private void dgv_list_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
var rowIdx = (e.RowIndex + 1).ToString();
var centerFormat = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, dgv_invList.RowHeadersWidth,
e.RowBounds.Height);
e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
}