C# Datagridview 首行自动显示行号——RowPostPaint事件

点击控件的事件按钮,找到RowPostPaint事件;

双击事件名称的右边空白部分创建事件的函数;

然后在函数中添加如图中的代码;

private void DGV_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rect = 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, rect,
                DGV.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

效果如下,行号都显示在首列。

Datagridview 首列添加行号的方法不唯一,还有其他的方法,各路大神可以多多尝试。

发布了20 篇原创文章 · 获赞 34 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/feiduan1211/article/details/82793273
今日推荐