如何基于组合框的值更改DataGridView单元格颜色? [英] How to change DataGridView cell color based on value of Combobox?

查看:112
本文介绍了如何基于组合框的值更改DataGridView单元格颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的datagridview:

I have a datagridview as below:

我想要:


  • 加载表单时,如果性别列的值为Male,列 Name 的相应颜色单元格为White

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

如果更改列性别的值:男性→女性,列名称的颜色单元将为DarkGray,
,否则如果更改列性别的值:女性→男性,列名称的颜色单元格将为白色

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.

推荐答案

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

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

}

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

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

这篇关于如何基于组合框的值更改DataGridView单元格颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆