c#Datagridview(Winform)基于相邻单元格值的单元格着色

时间:2016-03-31 07:38:56

标签: c# winforms datagridview

是否可以根据多个条件为datagridview单元着色。 我知道我可以根据单元格值更改单元格的颜色。但是可以添加条件,我也可以根据相邻的单元格值应用颜色。

要将单元格的日期与当前日期进行比较,我使用下面的代码。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DATE")
    {
        if (e.Value == null || e.Value == System.DBNull.Value || e.ColumnIndex < 0 || e.RowIndex < 0)
        {
            return;
        }
        else
        {
            if (((DateTime) e.Value).Date < (DateTime) DateTime.Now.Date)
            {
                e.CellStyle.BackColor = Color.Red;
                e.CellStyle.ForeColor = Color.White;
            }
        }
    }
    // This section change the color of action proposed description column cell.
    // i want to change the color in "ACTION PROPOSED DATE"column, if "ACTION PROPOSED DESCRIPTION" contains file closed
    else if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DESCRIPTION")
    {
        if (e.Value == null || e.Value == System.DBNull.Value || e.ColumnIndex < 0 || e.RowIndex < 0)
        {
            return;
        }
        else
        {
            string stringvalue = (string) e.Value;
            stringvalue = stringvalue.ToLower();
            if ((stringvalue.IndexOf("file closed") > -1))
            {
                e.CellStyle.BackColor = Color.Purple;
            }
        }
    }
}

我想改变&#34;行动提议日期&#34;列单元格为紫色,如果&#34;操作建议说明&#34;包含&#34;文件已关闭&#34;

这是我在datagridview

中得到的结果

present output

这是我期待的结果

expected result

发布之前我已经搜索了很多内容,但我没有找到任何问题的答案。所以我希望我没有重复这个问题。

2 个答案:

答案 0 :(得分:2)

CellFormatting事件的事件参数指示哪个单元格正在格式化。但您可以使用其他单元格来确定如何格式化该单元格。

例如,要实现目标,您可以使用以下内容:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
    if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DATE")
    {
        // Take the other column value for the same row
        var proposedDescription = dg.Rows[e.RowIndex].Cells["ACTION PROPOSED DESCRIPTION"].Value as string;
        if (proposedDescription != null && proposedDescription.IndexOf("file closed", StringComparison.CurrentCultureIgnoreCase) >= 0)
        {
             e.CellStyle.BackColor = Color.Purple;
             return;
        }
        if (e.Value == null || e.Value == System.DBNull.Value || e.ColumnIndex < 0 || e.RowIndex < 0)
        {
            return;
        }
        else
        {
            if (((DateTime) e.Value).Date < (DateTime) DateTime.Now.Date)
            {
                e.CellStyle.BackColor = Color.Red;
                e.CellStyle.ForeColor = Color.White;
            }
        }
    }
}

答案 1 :(得分:1)

您只需更改此行代码

即可
e.CellStyle.BackColor = Color.Purple;

// Note i did change the column name of ACTION PROPOSE DATE
// due to syntax property naming rules.
dataGridView1["ACTION_PROPOSED_DATE", e.RowIndex].Style.BackColor = Color.Purple

您还必须更改此内容:

else if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DESCRIPTION")

if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DESCRIPTION")

您的程序不会跳过检查您的列提议说明。

您可以添加一个条件,该条件还会检查相邻单元格值是否在此代码行下方文件已关闭:

if (((DateTime) e.Value).Date < (DateTime) DateTime.Now.Date) 
{ 
    e.CellStyle.BackColor = Color.Red; 
    e.CellStyle.ForeColor = Color.White; 
} 

并将当前单元格颜色设置为紫色。为此,请编写以下代码:

if (dataGridView1["ACTION_PROPOSED_DESCRIPTION", e.RowIndex].Value.ToString() == "File Closed") 
{ 
    dataGridView1["ACTION_PROPOSED_DATE", e.RowIndex].Style.BackColor = Color.Purple;
}