如何根据 Combobox 的值更改 DataGridView 单元格颜色?

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

我有一个数据网格视图如下:

我愿意:

我试过了,但我做不到:

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

或:

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

关于这些的任何提示都会有很大帮助。提前致谢。

要更改背景颜色,您必须订阅 CellFormatting 事件。然后将此代码添加到事件处理程序中:

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

}

要在 DataGridViewComboBoxCell 中选择新值时进行验证,请订阅 CurrentCellDirtyStateChanged 事件并在其处理程序中尝试此代码:

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

只是为了向您展示一个工作示例,我正在更改 ForeColor 而不是 Back,但概念是相同的。您需要应用默认值:

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

然后根据你的触发器是什么,你需要分配一个处理程序:

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

然后处理事件,它看起来像这样:

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

与接受的答案不同,这使用样式必须在一个地方更改定义。