datagridview单元格颜色 [英] datagridview cell colour

查看:161
本文介绍了datagridview单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在我的Winform(4.0)中,我正在使用datagridview.
悬停在单元格上方时,单元格的颜色变为黄色.
datagridview的DataGridViewCellStyle设置为:{BackColor = Color [A = 255,R = 244,G = 244,B = 244]}

鼠标经过具有现有颜色的单元格,然后单元格变为黄色,但是当鼠标离开该单元格时,单元格将失去其先前的颜色.
电池如何具有与变黄之前相同的颜色?
谢谢

Hi,
In my winform (4.0) I am using datagridview.
On hovering over the cells, the colour of the cell changes to yellow.
The DataGridViewCellStyle of the datagridview is set to : { BackColor=Color [A=255, R=244, G=244, B=244] }

The mouse goes over the cell which has an existing colour, then cell changes to yellow, BUT when the mouse moves away from the cell, the cell looses it''s previous colour.
How can the cell have the same colour as before it turned yellow?
Thanks

private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                if (dgv[e.ColumnIndex, e.RowIndex].Value != null)
                {
                    if (dgv[e.ColumnIndex, e.RowIndex].Style.BackColor == Color.SkyBlue)
                    {
                        
                    }
                    else
                    {
                        dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Gold;
                    }
                }
            }
        }

        private void dgv_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                if (dgv[e.ColumnIndex, e.RowIndex].Value != null)
                {
                    if (dgv[e.ColumnIndex, e.RowIndex].Style.BackColor == Color.SkyBlue)
                    {
                        
                    }
                    else
                    {
                        dgv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
                    }
                }
            }
        }

推荐答案

似乎您没有在更改之前保存单元格的先前颜色.

创建类型为Color的变量,例如cellPrevColor.在CellMouseEnter事件中,保存背景色,然后再进行更改.然后在CellMouseLeave事件中,将背景色设置为cellPrevColor的值.

另外,不要使用硬编码的颜色,而要使用网格的颜色属性.您可能要使用DefaultCellStyle.BackColor,而不是将单元格更改为Color.White.这样,如果出于任何原因更改默认单元格样式,则无需返回并重写代码块.
It looks like you are not saving the previous color of the cell before changing it.

Create a variable, say cellPrevColor, of type Color. In the CellMouseEnter event, save the back color before changing it. Then in the CellMouseLeave event, set the back color to the value of cellPrevColor.

Also, rather than using hard-coded colors, use the grid''s color properties. Instead of changing the cell to Color.White, you probably want to use DefaultCellStyle.BackColor. That way, if you were to change the default cell style for whatever reason, you will not need to go back and rewrite blocks of code.


这篇关于datagridview单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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