Changing datagridview cell color based on condition

前端 未结 12 985
粉色の甜心
粉色の甜心 2020-12-02 20:36

I have loaded the data from database to datagridview and have two columns target value and volume where volume >target value that volume cell should be in green color and

12条回答
  •  春和景丽
    2020-12-02 21:08

    Surprised no one mentioned a simple if statement can make sure your loop only gets executed once per format (on the first column, of the first row).

        private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // once per format
            if (e.ColumnIndex == 0 && e.RowIndex == 0)
            {
                foreach (DataGridViewRow row in dgv.Rows)
                    if (row != null)
                        row.DefaultCellStyle.BackColor = Color.Red;
            }
        }
    

提交回复
热议问题