DataGrid scrolling
-
How do you do anything useful with the
Scroll
event for aDataGrid
? I ned to take some action depending on which portion of the grid is visible, but there's noFirstVisibleRow
property, although there is aFirstVisibleColumn
. I could do this with the scroll bars in an MFC ScrollView, but I can't see it in .NET. -
How do you do anything useful with the
Scroll
event for aDataGrid
? I ned to take some action depending on which portion of the grid is visible, but there's noFirstVisibleRow
property, although there is aFirstVisibleColumn
. I could do this with the scroll bars in an MFC ScrollView, but I can't see it in .NET.Oh what, you're saying their example for the
Scroll
event in the SDK docs that pops up aMessageBox
every time you scroll isn't practical?! :) You could simulate aFirstVisibleRow
of sorts (if you're extendingDataGrid
, it would definitely make a good read-only property) by callingDataGrid.HitTest
with coordinates that would always be in the upper-left corner, like1, 1
.Microsoft MVP, Visual C# My Articles
-
Oh what, you're saying their example for the
Scroll
event in the SDK docs that pops up aMessageBox
every time you scroll isn't practical?! :) You could simulate aFirstVisibleRow
of sorts (if you're extendingDataGrid
, it would definitely make a good read-only property) by callingDataGrid.HitTest
with coordinates that would always be in the upper-left corner, like1, 1
.Microsoft MVP, Visual C# My Articles
-
But the
Scroll
event has aEventArgs
not aMouseEventArgs
so I can't do a hit test can I? If I could find the position of the slider (relative to top & bottom of the scrollbar) that would be enough.For one, you can get the mouse position in screen coordinates at any time from the static
Control.MousePosition
property, but you didn't read my reply so this really doens't make a difference. When you scroll, obviously a new row is displayed at the top of theDataGrid
, right? All you need to do is callHitTest
with the coordinates1, 1
(ornew Point(1, 1)
and that will get you the row currently at the top of theDataGrid
's view. This is a very simple workaround. If you want the slider position, simply P/Invoke theSendMessage
API and send theSBM_GETPOS
message to theDataGrid
control using itsHandle
property (theHWND
for the control). This will get you a value of between 0 and 100, so you'll need to take the row height into account and calculate everything, but the workaround above is so much simpler and doesn't require you to P/Invoke anything (making your code more portable).Microsoft MVP, Visual C# My Articles
-
For one, you can get the mouse position in screen coordinates at any time from the static
Control.MousePosition
property, but you didn't read my reply so this really doens't make a difference. When you scroll, obviously a new row is displayed at the top of theDataGrid
, right? All you need to do is callHitTest
with the coordinates1, 1
(ornew Point(1, 1)
and that will get you the row currently at the top of theDataGrid
's view. This is a very simple workaround. If you want the slider position, simply P/Invoke theSendMessage
API and send theSBM_GETPOS
message to theDataGrid
control using itsHandle
property (theHWND
for the control). This will get you a value of between 0 and 100, so you'll need to take the row height into account and calculate everything, but the workaround above is so much simpler and doesn't require you to P/Invoke anything (making your code more portable).Microsoft MVP, Visual C# My Articles
I did read your reply but didn't know that HitTest could be used for anything other than a mouse position - that's what the help for HitTest suggests. But I'm still not getting there...
DataGrid.HitTestInfo hti = valueGrid.HitTest(1,1); int row = hti.Row; if (row >=0) {.......
but row always gives -1 no matter where the scroll position is. I've also triedDataGrid.HitTestInfo hti = valueGrid.HitTest(valueGrid.Location);
but no joy, and even tried checking the point in the middle of the top left cell, but again always row = -1. Obviously I'm missing something very obvious!:confused: