C#winfrom中DataGridView实现行标题

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
    using (SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
    {
        Font font = this.dataGridView1.DefaultCellStyle.Font;
        string text = (e.RowIndex + 1).ToString();
        SizeF sizef = e.Graphics.MeasureString(text, font);
        float hwidth = this.dataGridView1.RowHeadersWidth;
        float hheight = this.dataGridView1.RowTemplate.Height;
        float arrowWidth = 10;
        float x = e.RowBounds.Location.X + arrowWidth + ((hwidth - arrowWidth) - sizef.Width) / 2;
        float y = e.RowBounds.Location.Y + (hheight - sizef.Height) / 2;
        e.Graphics.DrawString(text, font, b, x, y);
    }
}

 

posted @ 2022-04-12 22:46  Bridgebug  阅读(648)  评论(0编辑  收藏  举报