DataGrid random behavior when scrolling

I came across a post in the silverlight forums which shows the random behavior of the DataGrid. This happens obviously only when scrollbars are there and when we scroll. Wraping DataGridRowsPresenter in the Template of the DataGrid in a scrollviewer seems to get around this issue. As scrollviewer has some border margin etc. I styled it to remove all unnecessary stuff.

 <ScrollViewer Grid.ColumnSpan=”2″ Grid.Row=”1″ Style=”{StaticResource ScrollViewerStyle1}”>
                                        <sdk:DataGridRowsPresenter x:Name=”RowsPresenter” />

If you remove the style applied to the DataGrid in the sample you can clearly see the random behavior. You can download the sample here .


13 thoughts on “DataGrid random behavior when scrolling

      1. Hi Lee. Seems like this problem tails me everywhere I go. My situation now is a listbox with CheckBox configured as DataTemplate and the behavior is again manifesting. I thought I could use the fix for a DataGridView but this is not the case. As a matter or fact, when I looked at the markup in Blend, the ItemPresenter is already enclosed by a scrollviewer. Now I’m stuck. Any idea bro?

  1. Thanks a lot. This is great solution. But it kills ScrollIntoView method. Any idea how to fix that? Thank again

    1. Hi,
      Are you trying to scroll to a particular item or Top and Bottom. If later you can find the scrollviewer and use the Methods ScrolltoTop() etc. if scrolling to a Item, I am not sure.

      1. Yes, I’m trying to scroll to a particular item.

        CollectionViewGroup viewGroup = GetSelectedGroup();

        if (viewGroup == null)

        dataGrid.ScrollIntoView(viewGroup, null);
        dataGrid.ExpandRowGroup(viewGroup, true);

        I will try to fix that issue in the source code of the DataGrid control.


          1. Right after I posted my previous comment, I found a solution. I read more into Lee’s suggestion. Here is what I did:
            1. I added a name to the ScrollView (x:Name):

            2. I had this extention method for other purposes. Basically this method finds some control, within another control.
            public static T GetChildObject(this DependencyObject obj, string name) where T : DependencyObject
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            object c = VisualTreeHelper.GetChild(obj, i);
            if (typeof(T).IsAssignableFrom(c.GetType()) && (String.IsNullOrEmpty(name) || ((FrameworkElement)c).Name == name))
            return (T)c;
            object gc = ((DependencyObject)c).GetChildObject(name);
            if (gc != null)
            return (T)gc;

            return null;
            3. Now I extract the ScrollView
            ScrollViewer sv = dgDeviceUsers.GetChildObject(“svSomeName”);

            4. Now you can use methods of ScrollView like ScrollToBottom() or ScrollToTop(). Eg. sv.ScrollToBottom();

            It worked for me.

  2. Only problem with that solution is that it kills the virtualization in the datagrid.
    If you will try to add few thousands rows in the grid you’ll notice dramatic decrease in performance.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s