VB代码:
Private Sub DataGridViewX1_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewX1.RowPostPaint
Dim pRect As New Rectangle(e.RowBounds.Location, New Size(DataGridViewX1.RowHeadersWidth - 4, e.RowBounds.Height))
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), DataGridViewX1.RowHeadersDefaultCellStyle.Font, pRect, DataGridViewX1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.Right Or TextFormatFlags.VerticalCenter)
End Sub
C#代码
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dataGridView1.RowHeadersWidth - 4,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dataGridView1.RowHeadersDefaultCellStyle.Font,
rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}