C#学习笔记之-DataGridView事件-显示行号RowPostPaint

在使用 DataGridView控件 显示数据时,有时候需要显示行号,这时就要用到 DataGridView 的 RowPostPaint 事件:

  private void Dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 
  {
    DataGridView dgv = sender as DataGridView;
    Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,e.RowBounds.Location.Y, dgv.RowHeadersWidth - 4,e.RowBounds.Height);

  TextRenderer.DrawText(e.Graphics,(e.RowIndex + 1).ToString(),dgv.RowHeadersDefaultCellStyle.Font,rectangle,dgv.RowHeadersDefaultCellStyle.ForeColor,TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
  }

posted @ 2018-12-13 16:51  道友、请留步  阅读(921)  评论(0编辑  收藏  举报