DevExpress GridView 鼠标悬停颜色追踪(行或单元格)

博客主要介绍了DevExpress GridView鼠标悬停颜色追踪(行或单元格)的相关内容,探讨如何将GridView做成类似网页列表,实现鼠标移动时行背景颜色随之变化的悬停追踪效果,并给出了调用代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DevExpress GridView 鼠标悬停颜色追踪(行或单元格)

2019年07月12日 15:17:02 涛神-Dev 阅读数 41 标签: GridView行追踪单元格追踪 更多

个人分类: DevExpressWinformGridControl

view.RefreshData()

如何将GridView做成类似网页的列表那样,鼠标移动的是行背景颜色跟着变,也就是所为的鼠标悬停追踪,

效果如下:

代码如下:

public class ViewStyleHelper{
    bool enable;
    public bool Enable{
        get { return enable; }
        set{
            enable = value;
            UnRegisterEvent();
            if (enable){
                RegisterEvent();
            } else {
                View.RefreshData();
            }
        }
    }

bool byRow;

/// <summary>
/// 真为行,假为单元格
/// </summary>
public bool ByRow{

get { return byRow; }

set

{

byRow = value;

UnRegisterEvent();

if(enable)

RegisterEvent();

}

}

public DevExpress.XtraGrid.Views.Grid.GridView View { get; private set; }

/// <summary>
/// 当前列
/// </summary>
GridColumn currentCol;

/// <summary>
/// 当前行
/// </summary>
int currentRowHandle;

public ViewStyleHelper(DevExpress.XtraGrid.Views.Grid.GridView view,bool byRow=true){
    View = view;
    this.byRow = byRow;
    Enable = true;
    view.MouseLeave += (s, e) =>{
        currentCol = null;
        currentRowHandle = int.MinValue;
        view.RefreshData();
    };
}

void RegisterEvent(){
View.MouseMove += OnMouseMove;
if (!byRow)
View.RowCellStyle += OnRowCellStyle;
else
{
View.RowStyle += OnRowStyle;
}
}

void UnRegisterEvent(){
    View.MouseMove -= OnMouseMove;
    View.RowCellStyle -= OnRowCellStyle;
    View.RowStyle -= OnRowStyle;
}

private void OnMouseMove(object sender, MouseEventArgs e){
    var view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
    var info = view.CalcHitInfo(e.Location);
    bool refresh = false;
    if (currentCol != info.Column || currentRowHandle != info.RowHandle){
        refresh = true;
    }
    if (info.InDataRow){
        currentCol = info.Column;
        currentRowHandle = info.RowHandle;
    }else{
        currentCol = null;
        currentRowHandle = int.MinValue;
    }
    if (refresh)
        view.RefreshData();
    }

private void OnRowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e){
if (e.RowHandle == currentRowHandle)
{
e.Appearance.BackColor = Color.FromArgb(108, 178, 235);
e.HighPriority = true;
}
}

    private void OnRowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e){
        if (e.Column == currentCol && e.RowHandle == currentRowHandle){
            e.Appearance.BackColor = Color.FromArgb(108, 178, 235);
    
        }
    }
}

​​​​​调用代码:

ViewStyleHelper helper;

private void Form1_Load(object sender, EventArgs e){
    helper = new ViewStyleHelper(gridView1);
    var dt = CreateDt();
    gridControl1.DataSource = dt;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值