我遇到了我的DataGridView
問題,其中我使用CellFormatting方法根據其內容爲行重新着色。在大多數情況下,這似乎可以正常工作,直到DGV中的第一行返回true爲止 - 在這種情況下,DGV中的每一行都顯示爲紅色。C#DataGridView行着色
這裏是我的代碼:
private void MyData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow theRow = MyData.Rows[e.RowIndex];
if (theRow.Cells[4].Value.ToString() == "1")
{
ChangeRowColor(theRow, Color.PaleVioletRed);
}
}
private void ChangeRowColor(DataGridViewRow r, Color c)
{
r.DefaultCellStyle.BackColor = c;
}
編輯:解:
private void MyData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow theRow = MyData.Rows[e.RowIndex];
if ((int)theRow.Cells[4].Value == 1)
{
e.CellStyle.BackColor = Color.PaleVioletRed;
}
}
感謝您爲我設置格式,我是一個新手 – Krisisonfire
在else if子句之後添加'else {ChangeRowColor(theRow,Color.White);}'? – Pikoh
你能提供你的樣本數據嗎? – Berkay