Detect if the user scrolls the data scrollbar

I am updating dataGridView

with a new DataTable using

dataGridView1.DataSource = table

      

However, I don't want to do this when the user scrolls the dataGridView. How can I check if the scroll is scrolling or the scroll is complete (like dragging and not clicking)?

I looked at the Scroll event but it seems like it only fires the first time the scroll bar is pressed and scrolled. It looks like Google searches aren't too much of a problem.

+3


source to share


1 answer


I've done this in the past by subclassing the DataGridView class and using that instead of the 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);
    }
}

      

+4


source







All Articles