为 DataGridView 添加行号

   

 /// <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);
        }

 

posted @ 2013-06-13 14:43  双魂人生  阅读(318)  评论(0编辑  收藏  举报