如何更改单元格背色而不是更改整个单元格颜色? [英] How can I change cell backcolor instead changing entire cell color?

查看:88
本文介绍了如何更改单元格背色而不是更改整个单元格颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果结束日期达到实际日期(DateTime.Now),我想出了这个代码来更改单元格的颜色。我的问题是它改变了整行而不是第5列的细胞。任何想法?



我尝试过:



代码段:



I came up with this code to change the color of a cell if the end date is reaching the actual date(DateTime.Now). My problem is that it changed the entire row instead of the cell on column 5. Any ideias?

What I have tried:

Code snippet:

foreach (DataGridViewRow row in dgw.Rows)
                {
                    var now = DateTime.Now;
                    var cellDate = DateTime.Parse(row.Cells[5].Value.ToString());
                    var forTenDays = now.AddDays(+10);

                    if (now > cellDate)
                    {
                        row.DefaultCellStyle.BackColor = Color.Red;
                    }
                    else if ((now < cellDate) && (cellDate < forTenDays))
                    {
                        row.DefaultCellStyle.BackColor = Color.Yellow;
                    }
                    else
                    {
                        row.DefaultCellStyle.BackColor = Color.LightGreen;
                    }
                }

推荐答案

使用
row.Cells[5].Style.BackColor = Color.Red;

etc


这篇关于如何更改单元格背色而不是更改整个单元格颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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