简体   繁体   中英

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

I have a datagridview as below:

在此处输入图片说明

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.

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

Just to show you a working example, I am changing ForeColor instead of Back, but the concept is the same. You need to apply a default:

this.dgvUsers.DefaultCellStyle.ForeColor = Color.Black;

Then based on what your trigger is, you need to assign a handler:

dgvUsers.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dgvUsers_CellFormatting);
this.Controls.Add(dgvUsers);

Then to handle the event, it will look something like this:

// Handle user going inactive or being reactivated
private void dgvUsers_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataRowView row = dgvUsers.Rows[e.RowIndex].DataBoundItem as DataRowView;
    if (row != null && row.Row.ItemArray[7].Equals("N"))
    {
        e.CellStyle.ForeColor = Color.Gray;
    }
    else
    {
        e.CellStyle.ForeColor = Color.Black;
    }
}

Unlike the accepted answer, this uses styles to have to change defined in a single place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM