使用鼠标滚动 DataGridView

2024-04-19

因此,我们都熟悉单击并按住鼠标按钮,然后将鼠标移动到网格边缘,列/行滚动并且选择范围增加的功能。

我有一个基于 DataGridView 的控件,由于性能问题,我必须关闭 MultiSelect 并自行处理选择过程,现在单击+按住滚动功能也被禁用。

关于如何在此功能中回写有什么建议吗?

我正在考虑使用像 MouseLeave 事件这样简单的东西,但我不确定如何确定它离开的位置,以及实现动态滚动速度。


只需将此代码添加到您的Form1_加载

DataGridView1.MouseWheel += new MouseEventHandler(DataGridView1_MouseWheel);

这是针对 MouseWheel 事件的

void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
    int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex;
    int scrollLines = SystemInformation.MouseWheelScrollLines;

    if (e.Delta > 0) 
    {
        this.DataGridView1.FirstDisplayedScrollingRowIndex 
            = Math.Max(0, currentIndex - scrollLines);
    }
    else if (e.Delta < 0)
    {
        this.DataGridView1.FirstDisplayedScrollingRowIndex 
            = currentIndex + scrollLines;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用鼠标滚动 DataGridView 的相关文章

随机推荐