1. 程式人生 > >DataGridView中根據欄位取值顯示不同的行背景色

DataGridView中根據欄位取值顯示不同的行背景色

在dataGridView中根據不同的資料顯示不同的背景色便於區分資料,方便區分和檢視。基本方法就是遍歷所有行,根據欄位的區只要求,設定背景顏色。程式碼如下

 //根據列表中資料不同,顯示不同顏色背景
            foreach( DataGridViewRow dgRow in DGVAllRentList.Rows ){
               //判斷
                if ((double)(dgRow.Cells[5].Value) < 0.2 || (double)(dgRow.Cells[5].Value) > 0.8)
                {
                    //將這行的背景色設定成紅色
                    dgRow.DefaultCellStyle.BackColor = Color.Red;
                }
            }