C#DataGridView列头自增序号

2023-08-10  本文已影响0人  堆石成山

话不多说,在RowPostPaint事件中写入下面代码:

  private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            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);
        }

效果:


image.png
上一篇 下一篇

猜你喜欢

热点阅读