Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置datagridview单元格背景色_C#_Winforms_Datagridview - Fatal编程技术网

C# 设置datagridview单元格背景色

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

我试图检查活动列的条件是否等于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

如果不起作用,请检查您的条件是否被命中。

您可以尝试以下方法:

    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;
            }
        }
    }