简体   繁体   English

如何更改DataGridView行的背景颜色并在悬停时撤消?

[英]How to change the background color of the DataGridView row and undo on hover?

I think this is simple problem but the following code snippet not worked. 我认为这是一个简单的问题,但以下代码段不起作用。 If currnet row is green I want it to be lighter green else I want light blue. 如果currnet行为绿色,我希望它的颜色更浅,否则我希望颜色为浅蓝色。 When I MouseLeave, the colors must become the previous state. 当我使用MouseLeave时,颜色必须变为以前的状态。

In this code, whatever the color is, it becomes blue. 在这段代码中,无论颜色是什么,它都会变成蓝色。 When you move the mouse, it becomes white. 当您移动鼠标时,它会变成白色。 It seems to be working fine in debug mode (entering if statement). 它似乎在调试模式下工作正常(输入if语句)。

 private void dtgVeri_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.RowIndex == -1) return;

        Color colorToChange=Color.LightBlue;
        Color colorCurrent = dtgVeri.Rows[e.RowIndex].DefaultCellStyle.BackColor;

        if (colorCurrent == Color.LightGreen)
            colorToChange = Color.PaleGreen;

        dtgVeri.Rows[e.RowIndex].DefaultCellStyle.BackColor = colorToChange;
    }

    private void dtgVeri_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == -1) return;

        Color colorToChange = Color.White;
        Color colorCurrent = dtgVeri.Rows[e.RowIndex].DefaultCellStyle.BackColor;

        if (colorCurrent == Color.PaleGreen)
            colorToChange = Color.LightGreen;

        dtgVeri.Rows[e.RowIndex].DefaultCellStyle.BackColor = colorToChange;
    }

Solved problem using CellMouseEnter event instead of CellMouseMove . 使用CellMouseEnter事件而不是CellMouseMove解决了问题。 Because CellMouseMove is constantly checking and creating problems but we want control only once hover at Enter and Leave. 因为CellMouseMove一直在检查和创建问题,但是我们只希望将控件悬停在Enter和Leave上一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM