2015-05-26 109 views
0

我需要在DataGridView中为特定单元格着色。我试图做,但它不起作用。在datagridview中为单元格着色

dataGridView1.Rows[1].Cells[1].Style.BackColor = Color.Red; 

但对于整个列它的工作原理...

dataGridView1.Columns[2].DefaultCellStyle.BackColor = Color.Red; 

但我需要一个单元格,请如果你能帮助谢谢

+0

'dataGridView1.Rows [1] .Cells [1] .Style.BackColor = Color.Red;'在这里工作正常。什么是细胞类型? – TaW

+1

是的,应该工作,听起来像CellStyle设置回来莫名其妙。检查你的代码,如果你无法找到发生这种情况的地方,你可以添加'DataGridView1_CellStyleChanged'并设置一个断点并对其进行调试。 – Koryu

+0

应该是'dataGridView1_CellStyleContentChanged' ...太晚而无法编辑。 – Koryu

回答

1

试试这个

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
     { 
      DataGridViewCellStyle MakeItRed = new DataGridViewCellStyle(); 
MakeItRed.BackColor = Color.Red; 

//make a whole column red 
dataGridView1.Columns[1].DefaultCellStyle = MakeItRed; 

//make a specific cell red 
DataGridViewRow row2 = dataGridView1.Rows[2]; 
row2.Cells[2].Style = MakeItRed; 

     } 
+0

它可以工作,但是当dataGridView中的数据太多时,程序变得非常缓慢,因为它运行在每个单元格上。有没有其他方法可以做到这一点? – user2841795

相关问题