2013-05-04 73 views

回答

1

我假设您使用的是WinForms,并且您的列名为Superfrom。

foreach (DataGridViewRow row in dataGridView2.Rows) 
{ 
    DataGridViewCell cell = row.Cells[Superfrom.Index]; 
    if (cell.Value != null) 
     cell.Style.BackColor = Color.Red; 
    else 
     cell.Style.BackColor = Color.White; 
} 

您对空白的定义可能会有所不同。在这种情况下,用任何陈述将为您的空虚情况测试替换cell.Value != null

+0

感谢您的回应!我把代码的两个部分放在dataGridView2_RowPostPaint中,但它似乎只是将整行改为红色? – Krbaugh 2013-05-04 19:52:54

+0

另一个问题,我只需要Superfrom列上的单元格根据它们是否为空来更改。并非所有列 – Krbaugh 2013-05-04 20:03:45

+0

我已编辑我的帖子,以解决上述两个问题。让我知道是否还有其他问题。 – 2013-05-04 20:12:02

0
  foreach (DataGridViewRow row in dataGridView2.Rows) 
      { 
       DataGridViewCell cell = row.Cells[Superfrom.Index]; 
       if (cell.Value.ToString() != String.Empty) 
       { 
        cell.Style.BackColor = Color.Red; 
       } 
       else 
       { 
        cell.Style.BackColor = Color.White; 
       } 

      }