【问题标题】:How to change DataGridView cell color based on value of Combobox?如何根据 Combobox 的值更改 DataGridView 单元格颜色?
【发布时间】:2017-02-05 04:59:32
【问题描述】:

我有一个如下的datagridview:

我想要:

  • 表单加载时,如果Gender列的值为Male,则Name列对应的颜色单元格为白色

  • 如果更改Gender列的值:男性→女性,Name列的颜色单元格将为深灰色, 否则如果更改Gender 列的值:女性→男性,Name 列的颜色单元格将为白色

我试过了,但我做不到:

    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 event。那里的例子可以帮助你做你想做的事
  • This 可能会有所帮助。
  • 感谢您的回复。我不用DataTable 那个用DataGridViewRow 来显示数据,那怎么办?对不起,我是 C# 新手
  • @MinhKiyo 关注单元格格式化事件。
  • 伯凯:我用dataGridView_CellFormatting更新了我的问题,但它不能

标签: c# datagridviewcombobox


【解决方案1】:

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

与公认的答案不同,这使用了必须在一个地方定义的样式。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,这正是我想要运行的。感谢您的努力!
    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2015-06-11
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多