在datagridview上显示行号
1,先在加入如下代码
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dataGridView1.RowHeadersWidth - 4,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dataGridView1.RowHeadersDefaultCellStyle.Font,
rectangle,
dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
2,然后再在From1.Designer.cs文件中加入如下代码,就行了。
this.dataGridView1.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.dataGridView1_RowPostPaint);
本文介绍了一种在DataGridView控件中自定义显示行号的方法。通过简单的两步操作:首先编写事件处理函数来绘制行号,然后在设计文件中注册该事件处理函数,即可实现行号的自定义显示。
569

被折叠的 条评论
为什么被折叠?



