winform datagridview 添加行号。

private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
  

private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
var dgv = (DataGridView)sender;
if (dgv.RowHeadersVisible)
{
Rectangle rect = new Rectangle(e.RowBounds.Left, e.RowBounds.Top,
dgv.RowHeadersWidth, e.RowBounds.Height);
rect.Inflate(-2, -2);
TextRenderer.DrawText(e.Graphics,
(e.RowIndex + 1).ToString(),
e.InheritedRowStyle.Font,
rect, e.InheritedRowStyle.ForeColor,
TextFormatFlags.Right | TextFormatFlags.VerticalCenter
);

}
}


}

原文地址:https://www.cnblogs.com/micro-chen/p/5681856.html