是否可以根據多個條件爲datagridview單元格着色? 我知道我可以根據該單元格值更改單元格的顏色。但有可能添加條件,我也可以根據相鄰的單元格值應用顏色。c#基於相鄰單元格值的Datagridview(Winform)單元格着色
要比較單元格的日期和當前日期,我使用下面的代碼。
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;
}
}
}
}
我想改變顏色的「行動建議DATE」列單元格紫色,如果「行動建議描述」包含「文件關閉」
這是結果我的datagridview
得到這是結果我期待
在發佈之前,我搜索了很多,但沒有找到任何答案給我的問題。所以我希望我沒有重複這個問題。
請格式化你的建議的工作代碼 –