Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataGrid scrolling

DataGrid scrolling

Scheduled Pinned Locked Moved C#
csharpc++cssquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    quilkin
    wrote on last edited by
    #1

    How do you do anything useful with the Scroll event for a DataGrid? I ned to take some action depending on which portion of the grid is visible, but there's no FirstVisibleRow property, although there is a FirstVisibleColumn. I could do this with the scroll bars in an MFC ScrollView, but I can't see it in .NET.

    H 1 Reply Last reply
    0
    • Q quilkin

      How do you do anything useful with the Scroll event for a DataGrid? I ned to take some action depending on which portion of the grid is visible, but there's no FirstVisibleRow property, although there is a FirstVisibleColumn. I could do this with the scroll bars in an MFC ScrollView, but I can't see it in .NET.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Oh what, you're saying their example for the Scroll event in the SDK docs that pops up a MessageBox every time you scroll isn't practical?! :) You could simulate a FirstVisibleRow of sorts (if you're extending DataGrid, it would definitely make a good read-only property) by calling DataGrid.HitTest with coordinates that would always be in the upper-left corner, like 1, 1.

      Microsoft MVP, Visual C# My Articles

      Q 1 Reply Last reply
      0
      • H Heath Stewart

        Oh what, you're saying their example for the Scroll event in the SDK docs that pops up a MessageBox every time you scroll isn't practical?! :) You could simulate a FirstVisibleRow of sorts (if you're extending DataGrid, it would definitely make a good read-only property) by calling DataGrid.HitTest with coordinates that would always be in the upper-left corner, like 1, 1.

        Microsoft MVP, Visual C# My Articles

        Q Offline
        Q Offline
        quilkin
        wrote on last edited by
        #3

        But the Scroll event has a EventArgs not a MouseEventArgs 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.

        H 1 Reply Last reply
        0
        • Q quilkin

          But the Scroll event has a EventArgs not a MouseEventArgs 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.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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 the DataGrid, right? All you need to do is call HitTest with the coordinates 1, 1 (or new Point(1, 1) and that will get you the row currently at the top of the DataGrid's view. This is a very simple workaround. If you want the slider position, simply P/Invoke the SendMessage API and send the SBM_GETPOS message to the DataGrid control using its Handle property (the HWND 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

          Q 1 Reply Last reply
          0
          • H Heath Stewart

            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 the DataGrid, right? All you need to do is call HitTest with the coordinates 1, 1 (or new Point(1, 1) and that will get you the row currently at the top of the DataGrid's view. This is a very simple workaround. If you want the slider position, simply P/Invoke the SendMessage API and send the SBM_GETPOS message to the DataGrid control using its Handle property (the HWND 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

            Q Offline
            Q Offline
            quilkin
            wrote on last edited by
            #5

            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 tried DataGrid.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:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups