检测用户是否正在滚动dataGridView滚动条

时间:2014-10-15 15:58:09

标签: c# winforms datagridview

我正在使用

更新带有新DataTable的dataGridView
dataGridView1.DataSource = table

但是,当用户滚动dataGridView时,我不想这样做。如何检查滚动条是滚动还是滚动完成(即拖动而不是单击)?

我已经查看了Scroll事件,但它似乎只是在首次单击滚动条并且未完成时触发。谷歌搜索似乎也没有提出太多具体内容。

1 个答案:

答案 0 :(得分:6)

我过去通过对DataGridView类进行子类化并使用它而不是DataGridView来完成此操作。

public class DataGridViewEx : DataGridView
{
    public bool IsUserScrolling { get; private set; }

    private const int WM_HSCROLL = 0x0114;
    private const int WM_VSCROLL = 0x0115;
    private const int SB_ENDSCROLL = 8;

    public event EventHandler UserScrollComplete;

    protected virtual void OnUserScrollComplete()
    {
        EventHandler handler = UserScrollComplete;
        if (handler != null) handler(this, EventArgs.Empty);
    }

    protected override void WndProc(ref Message m)
    {
        // http://msdn.microsoft.com/en-us/library/windows/desktop/bb787575(v=vs.85).aspx
        // http://msdn.microsoft.com/en-us/library/windows/desktop/bb787577(v=vs.85).aspx
        if ((m.Msg == WM_HSCROLL) ||
            (m.Msg == WM_VSCROLL))
        {

            short loword = (short)(m.WParam.ToInt32() & 0xFFFF);

            if (loword == SB_ENDSCROLL)
            {
                IsUserScrolling = false;

                OnUserScrollComplete();
            }
            else
            {
                IsUserScrolling = true;
            }
        }
        base.WndProc(ref m);
    }
}