为 DataGridView 控件添加行号

虽然好像不经常用到,不过还是记下来防止以后用到

        /// <summary>
        /// 为 DataGridView 控件添加行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagridview1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                datagridview1.RowHeadersWidth,
                e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString("d2"),
                datagridview1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                datagridview1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);

        }

 

posted @ 2013-09-06 09:59  古兰色回忆  阅读(249)  评论(0编辑  收藏  举报