2016-11-03 77 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