1、添加行序号主要用到RowPostPaint直接绘制上去。
2、隔行显示不同颜色主要用到AlternatingRowsDefutCellstyle 属性
获取或设置应用于DataGridview的奇数行的默认单元格样式。
代码比较简单如下:
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { Rectangle myrec = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth - 5, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, myrec, dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); } private void DataGridviewSettings() { this.dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.PaleGreen; this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.PapayaWhip;//设置奇数行颜色 }
实现效果:
![87dcb61e7cd2ba61388952d53aa1c0a2.png](https://img-blog.csdnimg.cn/img_convert/87dcb61e7cd2ba61388952d53aa1c0a2.png)
喜欢请帮忙点个关注和赞,谢谢您的阅读。