How can I color rows in datagridview with condition C#
<p>I want with a condition : </p>

<ul>
<li>all rows have <code>bool_badge =0</code>  : color with <strong>RED</strong></li>
<li>all rows have <code>bool_badge=1</code>   : color with <strong>ForestGreen</strong></li>
</ul>

<p>I have a code Correct BUT just when i click for a cell specific </p>

<p>My code:  </p>

<pre><code>foreach (DataGridViewRow dr in dataGridView1.Rows)
        {              
            int row = this.dataGridView1.CurrentCell.RowIndex;
            string valeur = dataGridView1[2, row].Value.ToString();

            if (valeur == "0")
            {
                dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
            }
            else
            {
                dataGridView1.DefaultCellStyle.SelectionBackColor = Color.ForestGreen;
            }
        }
</code></pre>

<p>The Result :
1) <a href="https://i.stack.imgur.com/pA3vL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pA3vL.png" alt="enter image description here"></a>`</p>

<p>2)  <a href="https://i.stack.imgur.com/7VemZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7VemZ.png" alt="enter image description here"></a></p>

<p>But I want when i execute my application , the test begin if bool_badge 0 or 1, and i have for all the gridview : color RED or ForestGreen ,</p>

<p>I try this code:</p>

<pre><code>for (int i = 0; i &lt; dataGridView1.Rows.Count; i++)
        {                
            string valeur = dataGridView1[2, i].Value.ToString();

            if (valeur == "0")
            {
                dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
            }
            else
            {
                dataGridView1.DefaultCellStyle.SelectionBackColor = Color.ForestGreen;
            }
        }
</code></pre>

<p>But i have ERROR! </p>

<p>this is :</p>

<p><a href="https://i.stack.imgur.com/Z5Lcx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z5Lcx.png" alt="enter image description here"></a></p>

<p>How can i fix it?</p>

<p>Very thanks,</p>