c# winform中datagridview空间添加序号和表头“序号”

DataGridView dgv = new DataGridView();

dgv.TopLeftHeaderCell.Value = "序号";

private void grid_RowPostPaint(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
{
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                grid.RowHeadersWidth - 4,
                e.RowBounds.Height);
 
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dgv.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dgv.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
 }

或者

private void dataGridView1_RowPostPaint(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)

{
    SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor);
    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dgv.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
}
posted @ 2016-03-08 17:07  Ping.Zhang  阅读(3421)  评论(0编辑  收藏  举报