C# 设置datagridview单元格背景色
我试图检查活动列的条件是否等于false,它会将单元格颜色行设置为红色,但即使活动列为false,它也不会更改行颜色 这是显示的输出:C# 设置datagridview单元格背景色,c#,winforms,datagridview,C#,Winforms,Datagridview,我试图检查活动列的条件是否等于false,它会将单元格颜色行设置为红色,但即使活动列为false,它也不会更改行颜色 这是显示的输出: 您需要为特定单元格设置背景色 for(int col = 0; col < dgv_loadout.Columns.Count; col++) { [your_row].Cells[col].Style.BackColor = Color.Red; } for(int col=0;col
您需要为特定单元格设置背景色
for(int col = 0; col < dgv_loadout.Columns.Count; col++)
{
[your_row].Cells[col].Style.BackColor = Color.Red;
}
for(int col=0;col
如果不起作用,请检查您的条件是否被命中。您可以尝试以下方法:
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridView dg = sender as DataGridView;
foreach (DataGridViewRow item in dg.Rows)
{
//your condition
int id = Convert.ToInt32(item.Cells[0].Value);
if (id == 1)
{
dg.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Red;
}
}
}
我使用RowsAdded事件只是为了测试。我想您应该设置
DataGridViewRow
的样式,而不是DefaultCellStyle
。看看这个答案
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridView dg = sender as DataGridView;
foreach (DataGridViewRow item in dg.Rows)
{
//your condition
int id = Convert.ToInt32(item.Cells[0].Value);
if (id == 1)
{
dg.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Red;
}
}
}