datagridview行颜色自定义如何做

   2024-10-08 3880
核心提示:要自定义DataGridView的行颜色,可以使用DataGridView的RowsDefaultCellStyle属性来设置默认行样式,也可以在DataGridView的RowP

要自定义DataGridView的行颜色,可以使用DataGridView的RowsDefaultCellStyle属性来设置默认行样式,也可以在DataGridView的RowPrePaint事件中自定义每一行的颜色。

以下是两种方法的示例代码:

使用RowsDefaultCellStyle属性设置默认行样式:
dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray;dataGridView1.RowsDefaultCellStyle.ForeColor = Color.Black;
在RowPrePaint事件中自定义每一行的颜色:
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e){    if (e.RowIndex % 2 == 0)    {        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGray;    }    else    {        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;    }}

以上代码中,当行号为偶数时,设置行的背景颜色为浅灰色;当行号为奇数时,设置行的背景颜色为白色。您可以根据需求自定义每一行的颜色。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号