简体   繁体   中英

datagridview Scroll event not firing when scrolling using keyboard

I am trying to capture the moment when a user ends horizontal scrolling in a datagridview. I need this to reposition buttons in the grid's header.

What I have done so far is adding a scrollListener which I found on this link: How can I receive the "scroll box" type scroll events from a DataGridView?

This works very well, exept that scrolling using the keyboard does not fires the scroll event. When I hoover the mouse in my code over the Scroll event it states "Occurs when the scroll box has been moved by either a mouse or keyboard action". So the event should fire when scrolling with the keyboard, but it does not.

My code is this :

    bool addScrollListener(DataGridView dgv)
    {
        // capture horizonal scrolling and redirect to s_Scroll. Purpose is to redraw buttons after scrolling
        bool Result = false;

        Type t = dgv.GetType();
        PropertyInfo pi = t.GetProperty("HorizontalScrollBar", BindingFlags.Instance | BindingFlags.NonPublic);
        ScrollBar s = null;

        if (pi != null)
            s = pi.GetValue(dgv, null) as ScrollBar;

        if (s != null)
        {
            s.Scroll += new ScrollEventHandler(s_Scroll);

            Result = true;
        }

        return Result;
    }

    void s_Scroll(object sender, ScrollEventArgs e)
    {
        // if grid is done scrolling horizontal, than redraw our buttons
        if (e.Type == ScrollEventType.EndScroll)
        {
            // code works well, but only get here when scrolling with mouse
            PositionButtons();
        }
    }

So my problem is that when the user scrolls using the mouse the s_Scroll event is fired, but when scrolling using the keyboard the s_Scroll event is not fired at all.

My question is how do I fix this so the event gets fired in both cases, and if that is not possible is there another way to capture the end of a horizontal scroll from a datagridview.

In DataGridView , keyboard actions are handled by the DataGridView to update the current cell position.

You must use ScrollBar.ValueChanged event and compare ScrollBar.Value with ScrollBar.Maximum to do what you want.


You have another solution to do what you want instead of adding an event listener on ScrollBar.Scroll : handle the DataGridView.Scroll event and verify the datagridview is scrolled down by using DataGridView.FirstDisplayedScrollingRowIndex property and DataGridView.DisplayedRowCount(true) method.

That would be simpler and safer.

private void TransactionsDGV_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.End)
    {
        e.Handled = true;
        DataGridViewCell cell = TransactionsDGV.Rows[TransactionsDGV.Rows.Count-2].Cells[0];
        TransactionsDGV.CurrentCell = cell;
        TransactionsDGV.BeginEdit(true);
        TransactionsDGV.EndEdit();
    }

    if (e.KeyCode == Keys.Home)
    {
        e.Handled = true;
        DataGridViewCell cell = TransactionsDGV.Rows[0].Cells[0];
        TransactionsDGV.CurrentCell = cell;
        TransactionsDGV.BeginEdit(true);
        TransactionsDGV.EndEdit();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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