DotNet · 2022年5月26日

WinForm中DataGridView控件在行头显示行号

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);
}

最新电影,电视剧,尽在午夜剧场

电影电视剧午夜不寂寞