/// <summary>为 DataGridView 添加行号。</summary> public static void ShowRows_DataGridView_RowPostPaint(DataGridView dgv, object sender, DataGridViewRowPostPaintEventArgs e) { 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); }
触发DataGrilView的RowPostPaint事件
private void dgvMember_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { CommonTools.ShowRows_DataGridView_RowPostPaint(this.dgvMember, sender, e); }