为 DataGridView 控件添加行号

时间:2023-03-09 15:28:39
为 DataGridView 控件添加行号

虽然好像不经常用到,不过还是记下来防止以后用到

        /// <summary>
/// 为 DataGridView 控件添加行号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void datagridview1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
datagridview1.RowHeadersWidth,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + ).ToString("d2"),
datagridview1.RowHeadersDefaultCellStyle.Font,
rectangle,
datagridview1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right); }