繁体   English   中英

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

[英]c# Datagridview (Winform) cell coloring based on adjacent cell value

是否可以根据多个条件为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;
            }
        }
    }
}

我想将“ACTION PROPOSED DATE”列单元格中的颜色更改为紫色,如果“ACTION PROPOSED DESCRIPTION”包含“file closed”

这是我在datagridview中得到的结果

目前的产出

这是我期待的结果

预期结果

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

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

您只需更改此行代码即可

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM