DataGridView控件在显示数据时,我们有时候需要显示行号,以便检索查看方便使用。
但DataGridView默认没有设置显示行号的属性。
此时我们只要在DataGridView的RowPostPaint事件中进行绘制,实现其效果:
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor); e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4); }
试试看!!