根据Datagridview中的两个单元格数据值更改单元格背景颜色 [英] Changing cell background color based on two cell data value in Datagridview
本文介绍了根据Datagridview中的两个单元格数据值更改单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个动态绑定的数据网格视图。它包含如下屏幕截图的数据。
图片链接
http://i.stack.imgur.com/RZOSN.jpg [ ^ ]
现在问题是我想根据数据值更改单元格颜色。我想扣除tamount-paymentamont,如果它> = 1,那么我想将这两个单元格颜色设置为红色,其他为绿色。每个新数据绑定。
I have one data-grid view which is bind dynamically. and it contain data like below screen shot.
Image Link
http://i.stack.imgur.com/RZOSN.jpg[^]
Now problem is i want change cell color based on data value. i want to deduct tamount-paymentamont and if it >=1 then i want to set this two cell color as a red and other as green.at every new data bind.
推荐答案
我试试这个,它对我有用..
i try this and it work for me..
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;
}
}
这篇关于根据Datagridview中的两个单元格数据值更改单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文