[英]Winform C# Update DataGridView on DataTable Cell value changed
[英]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.