tags:

views:

1918

answers:

5

Hi, I think I am stupid. I searched now for 15 minutes, and found several different solutions for scrolling on datagrids, but none seems to work for me.

I am using WPF with .NET 3.5 and the WPF Toolkit DataGrid. My grid gets updated when my observable collection changes, works perfectly. Now, my DataGrid is located inside a normal Grid and scrollbars appear if the DataGrid gets to big. Also fine...

And know, the 1.000.000 $ question:

How do I get the datagrid to scroll to the last row. There is:

  • no AutoScroll Property
  • no CurrentRowSelected Index
  • a CurrentCell, but no Collection I could use for CurrentCell = AllCells.Last

Any ideas? I feel really stupid, and it seems strange that this question is so hard. What am I missing?

A: 

What you need is to get the reference to the ScrollViewer object for your DataGrid. You can then manipulate the VerticalOffset property to scroll to the bottom.

To add even more flare to your app...you could add a Spline animation to the scroll so everything looks up to par with the rest of the application.

Justin Niessner
Mm, ok, if found a few attached properties, like ScrollViewer.CanContentScroll = "True", but without any effect. I know, RTFM, but hey, I still got hope for a line of code here. I know, spoiled brat :-)
Christian
+4  A: 

;)

        if (mainDataGrid.Items.Count > 0)
        {
            var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as Decorator;
            if (border != null)
            {
                var scroll = border.Child as ScrollViewer;
                if (scroll != null) scroll.ScrollToEnd();
            }
        }
Joseph Melettukunnel
Thanks a lot, if only life would always be that easy :-)
Christian
Brilliant piece of code, wrap it in an ArgumentOutOfRange exception and it would be perfect, for when the listbox might be empty.
wonea
+3  A: 

You should use the datagrid method

datagrid.ScrollIntoView(itemInRow);

or

datagrid.ScrollIntoView(itemInRow, column);

this way provides no messing around finding the scroll viewer etc.

Aran Mulholland
A: 

listbox.add bla

listbox.selectedindex = count -1

listbox.scrollintoview(listbox.selecteditem)

listbox.selectedindex = -1

:)

azze
A: 

if large data datagrid.ScrollIntoView(itemInRow, column); not works fine then we need to use below one only:

if (mainDataGrid.Items.Count > 0) 
        { 
            var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as Decorator; 
            if (border != null) 
            { 
                var scroll = border.Child as ScrollViewer; 
                if (scroll != null) scroll.ScrollToEnd(); 
            } 
        } 
TRS Rao