与其他字段进行比较时,gridview上的背景颜色发生变化 [英] Background Color change on a gridview when comparing to fields to one another

查看:89
本文介绍了与其他字段进行比较时,gridview上的背景颜色发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a DataGridView, but I have to compare two fields to one another but when the one field is greater than the other the field has to change to a certain color, how can I write this method in 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;
}

这篇关于与其他字段进行比较时,gridview上的背景颜色发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆