Search code examples
c#datagridviewcombobox

How to change DataGridView cell color based on value of Combobox?


I have a datagridview as below:

enter image description here

I would like:

  • When the form load, if the Gender column's value is Male, the corresponding color cell of column Name will be White

  • When if changes the value of the column Gender: Male → Female, color cell of the column Name will be DarkGray, otherwise if changes the value of the column Gender: Female → Male, color cell of the column Name will be White

I tried it but I am not able to do it:

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

OR:

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

Any tips on these will be great help. Thanks in advance.


Solution

  • To change the Background color you must subscribe to the CellFormatting event. Then add this code to the event handler:

    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 && e.Value.ToString().Trim() == "Male")
            {
                dgv.Rows[e.RowIndex].Cells["name"].Style.BackColor = Color.White;
            }
            else
            {
                dgv.Rows[e.RowIndex].Cells["name"].Style.BackColor = Color.DarkGray;
            }
        }
    
    }
    

    To cause a validation when a new value is selected in your DataGridViewComboBoxCell,subscribe to the CurrentCellDirtyStateChanged event and try this code in its handler:

    private void dataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        DataGridView dgv = sender as DataGridView;
        DataGridViewCell cell = dgv.CurrentCell;
        if (cell is DataGridViewComboBoxCell)
        {
            dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            dgv.EndEdit();
        }
    }