C# 将字段相互比较时,gridview上的背景颜色会发生变化

C# 将字段相互比较时,gridview上的背景颜色会发生变化,c#,datagridview,C#,Datagridview,我有一个DataGridView,但我必须将两个字段相互比较 但是,当一个字段大于另一个字段时,字段必须更改为某种颜色,我如何用C#编写此方法: 这是一个简单的例子: private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { DataGridView dgv = dataGridView1; if (e.ColumnIndex < 0 || e.

我有一个DataGridView,但我必须将两个字段相互比较 但是,当一个字段大于另一个字段时,字段必须更改为某种颜色,我如何用C#编写此方法:


这是一个简单的例子:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    DataGridView dgv = dataGridView1;
    if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
    if (dgv[0, e.RowIndex].Value == null ||dgv[1, e.RowIndex].Value == null) return;
    // assuming integers, adapt to real types and real column indices!
    dgv[1, e.RowIndex].Style.BackColor = 
                            (int)dgv[0, e.RowIndex].Value < (int)dgv[1, e.RowIndex].Value ?
                                        Color.LightSalmon : dgv.DefaultCellStyle.BackColor;
}
private void dataGridView1\u CellPainting(对象发送方,DataGridViewCellPaintingEventArgs e)
{
DataGridView dgv=dataGridView1;
如果(e.ColumnIndex<0 | | e.RowIndex<0)返回;
if(dgv[0,e.RowIndex].Value==null | | dgv[1,e.RowIndex].Value==null)返回;
//假设整数,适应实数类型和实数列索引!
dgv[1,e.RowIndex]。Style.BackColor=
(int)dgv[0,e.RowIndex]。值<(int)dgv[1,e.RowIndex]。值?
Color.LightSalmon:dgv.DefaultCellStyle.BackColor;
}

您能提供您当前的代码吗?请编辑您的答案并将代码放在那里,至少告诉我们您指的是哪两个字段!仅仅一个屏幕截图并不能说明什么。不要将
DataGridView
a
GridView
!!这是错误的,令人困惑的,肯定会得到错误的答案,浪费大家的时间。。总是用正确的名字来称呼事物!使用CellPaint事件设置颜色!