简体   繁体   English

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

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

Is it possible to color datagridview cell based on multiple conditions. 是否可以根据多个条件为datagridview单元着色。 I know I can change the color of the cells based on that cell value. 我知道我可以根据单元格值更改单元格的颜色。 but is it possible to add condition where i can also apply color based on adjacent cell value. 但是可以添加条件,我也可以根据相邻的单元格值应用颜色。

To compare the cell's date with current date i am using the code below. 要将单元格的日期与当前日期进行比较,我使用下面的代码。

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

I want to change the color in the "ACTION PROPOSED DATE" column cell to purple , if "ACTION PROPOSED DESCRIPTION" contains "file closed" 我想将“ACTION PROPOSED DATE”列单元格中的颜色更改为紫色,如果“ACTION PROPOSED DESCRIPTION”包含“file closed”

this is the result i get in datagridview 这是我在datagridview中得到的结果

目前的产出

this is the result i am expecting 这是我期待的结果

预期结果

Before posting I have googled a lot, but I didn't get find any answer to my question. 在发布之前我已经google了很多,但我没有找到任何答案我的问题。 So I hope I didn't repeat this question. 所以我希望我没有重复这个问题。

The event args of the CellFormatting event indicate which cell is being formatted . CellFormatting事件的事件参数指示正在格式化哪个单元格。 But you can use other cells to determine how to format that cell. 但您可以使用其他单元格来确定如何格式化该单元格。

For instance, to achieve your goal, you can use something like this: 例如,要实现目标,您可以使用以下内容:

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

You can simply change this line of code 您只需更改此行代码即可

e.CellStyle.BackColor = Color.Purple;

to

// 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

You have to change this also: 你还必须改变这个:

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

to

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

that your program wont skip on checking your column proposed description. 您的程序不会跳过检查列提议的描述。

You can add a condition that will also check if the adjacent cell value is file closed below this line of codes: 您可以添加一个条件,该条件还将检查相邻单元格值是否在此代码行下方文件关闭:

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

and make the current cell color to purple. 并使当前单元格颜色为紫色。 To do that write this code: 为此,请编写以下代码:

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.

相关问题 Winform C#更新DataTable单元上的DataGridView值已更改 - Winform C# Update DataGridView on DataTable Cell value changed 访问单元格datagridview Winform C#的内容 - Access the content of a cell datagridview Winform C# 使用C#在Winform中合并DataGridView中的单元格? - Merge cell in datagridview in winform using c#? 当我单击 C# 中相同 winForm 中其他 datagridview 中的单元格时,在 datagridview 中的 Datagridviewcomboboxcell 中设置一个值 - Set a value in a Datagridviewcomboboxcell in datagridview when i click a cell in other datagridview in the same winForm in C# 取消双击DataGridView C#WinForm上的编辑单元格 - Cancel Edit Cell on Double Click DataGridView C# WinForm c#winform datagridview单元格单击到TabControl页面 - c# winform datagridview cell click to TabControl Page 通过单击 c#(winform) 更改 datagridview 单元格颜色 - Change datagridview cell color by clicking in c#(winform) 如何查找与当前单元格相邻的单元格的值C#+ Excel - How to find the value of a cell adjacent to the current cell C# + Excel 根据 excel 表中的值获取单元格范围/位置,并在 c# 中为单元格数据网格视图着色 - Get cell range/location based on value at excel sheet and colour the cell datagridview in c# C#将dataGridView单元格值应用于DateTimePickerObject - C# Applying dataGridView Cell Value to DateTimePickerObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM