Skip to main content
 首页 » 编程设计

c#之如何更改数据网格特定行的颜色

2024年02月24日22kuangbin

我要将数据表绑定(bind)到数据网格 我只想根据数据表行上的值更改特定数据网格行的颜色。我需要它的 c#/.net 代码

请您参考如下方法:

类似于:

this.dataGridView1.Rows[RowIndex].DefaultCellStyle.BackColor = Color.Yellow; 

或者如果你想在鼠标移动事件上使用它

private void DataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) 
{ 
dataGridView1[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Red; 
} 
 
private void DataGridView_CellLeave1(object sender, DataGridViewCellEventArgs e) 
{ 
dataGridView1[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Blue; 
} 

此外,Change individual DataGridView row colors based on column value可能有帮助。