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

3

我有一个datagridview如下:

enter image description here

我希望:

  • 当表单加载时,如果Gender列的值为Male,则列Name的相应颜色单元格将为White

  • 当更改列Gender的值时:Male → Female,列Name的颜色单元格将为DarkGray, 否则,如果更改列Gender的值:Female → Male,列Name的颜色单元格将为White

我尝试了但是无法实现:

    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事件。那里的示例可能会帮助您完成所需的操作。 - Pikoh
这个可能会有帮助:http://stackoverflow.com/questions/39540935/for-a-windows-forms-application-using-datagridview-how-can-i-check-a-value-in/39541363#39541363 - Berkay Yaylacı
谢谢您的回复。我不使用DataTable来显示数据,而是使用DataGridViewRow,那么应该怎么做呢?很抱歉我是C#的新手。 - MinhKiyo
@MinhKiyo 关注单元格格式化事件。 - Berkay Yaylacı
Pikoh:是的,完全正确,它可以正常运行。但必须点击其他单元格,新颜色才会更新。有没有办法在组合框的值更改时立即更新颜色? - MinhKiyo
显示剩余7条评论
2个回答

5

要更改背景颜色,您必须订阅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();
    }
}

是的,这正是我想要运行的。谢谢你的努力! - MinhKiyo

0

只是为了向您展示一个工作示例,我更改的是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;
    }
}

与被接受的答案不同,这个方法使用样式来实现在单一位置定义更改。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接