How to change DataGridView cell color based on value of Combobox?
<p>I have a datagridview as below:</p>

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

<p>I would like:</p>

<ul>
<li><p>When the form load, if the <code>Gender</code> column's value is Male, the corresponding color cell of column <code>Name</code> will be White</p></li>
<li><p>When if changes the value of the column <code>Gender</code>: Male → Female, color cell of the column <code>Name</code> will be DarkGray,
otherwise if changes the value of the column <code>Gender</code>: Female → Male, color cell of the column <code>Name</code> will be White</p></li>
</ul>

<p>I tried it but I am not able to do it:</p>

<pre><code>    private void dataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        DataGridView dgv = sender as DataGridView;
        DataGridViewCell cell = dgv.CurrentCell;

        if (dgv.Rows[cell.RowIndex].Cells["Gender"].Value.ToString().Trim() == "Male")
        {
            // Male
            dgv.Rows[cell.RowIndex].DefaultCellStyle.BackColor = Color.White;
        }
        else
        {
            // Female
            dgv.Rows[cell.RowIndex].DefaultCellStyle.BackColor = Color.DarkGray;
        }
    }
</code></pre>

<p>OR:</p>

<pre><code>    private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        DataGridView dgv = sender as DataGridView;

        if (dgv.Columns[e.ColumnIndex].Name.Equals("Gender"))
        {
            if (e.Value != null &amp;&amp; e.Value.ToString().Trim() == "Male")
            {
                e.CellStyle.BackColor = Color.White;
            }
            else
            {
                e.CellStyle.BackColor = Color.DarkGray;
            }
        }

        //if (dgv.Rows[e.RowIndex].Cells["Gender"].Value.ToString().Trim() == "Male")
        //{
        //    e.CellStyle.BackColor = Color.White;
        //}
        //else
        //{
        //    e.CellStyle.BackColor = Color.DarkGray;
        //}
    }
</code></pre>

<p>Any tips on these will be great help. Thanks in advance.</p>