DataGridView控件在显示数据时,我们有时候需要显示行号,以便检索查看方便使用。

DataGridView显示行号-RowPostPaint

但DataGridView默认没有设置显示行号的属性。

此时我们只要在DataGridView的RowPostPaint事件中进行绘制,实现其效果:

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }


试试看!!

相关文章:

  • 2021-08-16
  • 2022-01-16
  • 2022-12-23
  • 2022-01-13
  • 2021-08-21
猜你喜欢
  • 2021-08-21
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
相关资源
相似解决方案