[英]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.