2013-05-04 72 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; 
       } 

      }