根据Datagridview中的两个单元格数据值更改单元格背景颜色

时间:2013-09-19 05:51:47

标签: datagridview c#-3.0 desktop-application

我有一个动态绑定的数据网格视图。它包含如下屏幕截图的数据。

Data-Grid View

现在问题是我想根据数据值更改单元格颜色。 我想扣除tamount-paymentamont,如果它> = 1,那么我想在每次新数据绑定时将这两个单元格颜色设置为red,将其他颜色设置为green

我试试这个Answer但不适合我。

1 个答案:

答案 0 :(得分:0)

我在绑定时尝试这个

  for (int n = 0; n < (dataGridView1.Rows.Count - 1); n++)
            {
                double i = Convert.ToDouble(dataGridView1.Rows[n].Cells["tamount"].Value.ToString().Replace('.', ','));
                double j = Convert.ToDouble(dataGridView1.Rows[n].Cells["paymentamount"].Value.ToString().Replace('.', ','));
                double total = i - j;
                if (total >= 1)
                {
                    dataGridView1.Rows[n].Cells["tamount"].Style.BackColor = Color.LightPink;
                    dataGridView1.Rows[n].Cells["paymentamount"].Style.BackColor = Color.LightPink;

                }
                else
                {
                    dataGridView1.Rows[n].Cells["tamount"].Style.BackColor = Color.LightGreen;
                    dataGridView1.Rows[n].Cells["paymentamount"].Style.BackColor = Color.LightGreen;
                }

            }
相关问题