为Winform程序中DataGridView控件增加自动显示行号功能

网上找了几种显示行号的方法,采用了其中一种,在行头显示行号,很实用,如图:

代码如下:

private void gvTransferList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, gvTransferList.RowHeadersWidth, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                gvTransferList.RowHeadersDefaultCellStyle.Font, rectangle, gvTransferList.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
posted @ 2010-10-16 11:47  缤纷夏日  阅读(2890)  评论(0编辑  收藏  举报