2016-11-03 86 views
0

我遇到了我的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; 
     } 
} 
+0

感謝您爲我設置格式,我是一個新手 – Krisisonfire

+0

在else if子句之後添加'else {ChangeRowColor(theRow,Color.White);}'? – Pikoh

+0

你能提供你的樣本數據嗎? – Berkay

回答

0

您應該將背景色分配給該事件的CellStyle。

+0

不是我在做什麼? – Krisisonfire

+0

在參數e中你有屬性CellStyle。這是你在那裏的單元格和行的風格。 –

+0

你是一個天才,我很卑鄙 – Krisisonfire