2016-09-09 21 views
0

我有訂單管理桌面應用程序,我顯示所有在dgridview中的訂單。Datagridview設置行bg顏色,如果表達式爲真

所有訂單有三種類型:Paid,Not Paid,In progress

所以我試圖在datagridview列表中將bacground顏色更改爲僅在所有行中爲紅色(如果訂單具有狀態類型Not Paid)。黃河上的所有訂單,其中是狀態In prigress ...

所以做我可以在裏面循環(的foreach)與DataGridViewRow檢查山坳值Not Paid紅色bacground顏色?或者任何其他方式來設置此

回答

1

訂閱CellFormatting事件。

private void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (e.ColumnIndex == 1) // specify the desired column number 
    { 
     string value = e.Value.ToString(); 

     if (value == "Not Paid") 
      e.CellStyle.BackColor = Color.Red; 
     else if (value == "In Progress") 
      e.CellStyle.BackColor = Color.Yellow; 
    } 
} 
+0

非常感謝!它工作。只有一個我認爲我需要做的就是檢查'e.COlumng.Index!= null'。如果我得到'NullException',我就可以使用它了 – Ivan