Changing datagridview cell color based on condition

前端 未结 12 1039
粉色の甜心
粉色の甜心 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 20:57

    make it simple

    private void dataGridView1_cellformatting(object sender,DataGridViewCellFormattingEventArgs e)
    {
         var amount = (int)e.Value;
    
         // return if rowCount = 0
         if (this.dataGridView1.Rows.Count == 0)
             return;
    
         if (amount > 0)
             e.CellStyle.BackColor = Color.Green; 
         else
             e.CellStyle.BackColor = Color.Red;
    
    }
    

    take a look cell formatting

提交回复
热议问题