实现方式如下:
private void dgvRelation_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
try
{
SolidBrush b = new SolidBrush(this.dgvRelation.RowHeadersDefaultCellStyle.ForeColor);
e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture),
this.dgvRelation.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
}
catch { return; }
}
效果如下:
注意:使用该方式处理编号列的显示,需要处理cell_click事件,如: if (e.RowIndex == -1||e.ColumnIndex==-1) return;