How can I get scroll types of scroll type from a DataGridView?

I have a DataGridView and I am listening to its Scroll event. This gives me a ScrollEventArgs object whose type member should tell me the type of scroll event that occurred. On the MSDN documentation page , it says that I should be able to detect the movement of the scroll window while listening for events of types ThumbPosition, ThumbTrack, First, Last, and EndScroll.

However, when I drag the scroll window, I get events like LargeDecrement and LargeIncrement.

How to access ThumbPosition, ThumbTrack, First, Last and EndScroll events?

+7
c # scroll winforms datagridview
source share
2 answers
using System.Reflection; using System.Windows.Forms; bool addScrollListener(DataGridView dgv) { bool ret = false; Type t = dgv.GetType(); PropertyInfo pi = t.GetProperty("VerticalScrollBar", 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); ret = true; } return ret; } void s_Scroll(object sender, ScrollEventArgs e) { // Hander goes here.. } 

As you would expect, if you want to listen to horizontal scroll events, you change "VerticalScrollBar" to "HorizontalScrollBar"

+9
source share

You can do this without using reflection by referring to the horizontal or vertical scroll bar in the DataGridView control as follows. Replace HScrollBar with VScrollBar to get a vertical scrollbar.

 public MyFormConstructor() { InitializeComponent(); AddScrollListener(MyGrid, MyScrollEventHandler); } private void AddScrollListener(DataGridView dgv, ScrollEventHandler scrollEventHandler) { HScrollBar scrollBar = dgv.Controls.OfType<HScrollBar>().First(); scrollBar.Scroll += scrollEventHandler; } private void MyScrollEventHandler(object sender, ScrollEventArgs e) { // Handler with e.Type set properly } 
+4
source share

All Articles