Posted by: beatkiener | September 20, 2009

DataGridRow.GetIndex returns wrong index when inserting items to observable collection

This week I ran into a very strange behavior with the Silverlight DataGrid.

I added new items via an ObservableCollection to a set of existing items which are already bound to a standard DataGrid. If the new items are inserted on a lower index than the currently displayed range of items the result of the DataGridRow.GetIndex is not correct.
This has several side effects:

  • The hover effect does not work correct
  • Some rows lose the entire hover behavior
  • The alternating row background is set wrong
  • DataGridRow.GetIndex returns old index
  • Scrollbar jumps sometimes during thumb-track (I was not able to isolate this so far…)

 

It’s hard to explain all these in detail; therefore it created a short video.

image 

The remove notification from the observable collection works perfect (see last section in the video). All gets updated including the result of GetIndex and the AlternatingRowBackground for each row is correct.

I guess this is a misbehavior in the DataGrid, because of the fact that the Remove operation is working correctly.

First of all: fully rebind the DataGrid is not a solution for me, because the DataGrid is updating itself every few minute (remove/insert/add rows) from a service.

Can someone from the Silverlight team verify my assumption?

Silverlight Version is 3.0.40818.0.

Source code and Demo.

Thanks in advance,
Beat Kiener

Advertisement

Responses

  1. If I use the build-in Datagrid control instead of EvdDataGrid then the hover, altrenative row background are working fine. I think it happens only for EvdDataGrid .

    • The Microsoft Silverlight Team confirmed it as a bug. See the last comment in the following thread: http://forums.silverlight.net/forums/p/129843/290689.aspx#290689

      It did not any further work in this problem. Silverlight 4 Beta 1 will be shipped soon, I think tomorrow at the PDC09 (www.microsoftpdc.com). Hopfully the bug will be fiexed in this version.

  2. Hi Michael,

    Unfortunately not. I changed it to the build-in DataGrid and the same misbehavior happens. The only reason for EvdDataGrid subclass is to the get an instance of the rowpresenter in the OnApplyTemplate override.

    Could you post me your solution?

    Best regards,
    Beat

  3. This is the code of my datagrid subclass

        public class EvdDataGrid : DataGrid
        {
    
            internal DataGridRowsPresenter rowPresenter;
    
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
    
                // get row presenter
                this.rowPresenter = GetTemplateChild("RowsPresenter") as DataGridRowsPresenter;
            }
    
            internal int[] GetRowsInView()
            {
                /* *****************************************************************************************
                 * Returns wrong indexes if items where added prior to the top row visible in the grid
                 ******************************************************************************************/
                var query = from DataGridRow row in this.rowPresenter.Children
                            where row.Visibility == Visibility.Visible
                            orderby row.GetIndex()
                            select row.GetIndex();
    
                return query.ToArray();
            }
        }
    
  4. I am having a similar issue. All of the symptoms from your video present themselves. However, I am not adding rows to my grid. All of my rows are added in one shot. However, I am swapping out each object in my observable collection for an updated version of that object as data gets loaded. I am not sure if that helps. The issue is defintiely in the DataGridRowPresenter, and simply manifests itself in the DataGrid because it is using the presenter. Have you gotten any further with this? Please let me know if you have found a workaround or if you hear anything from the Silverlight development staff.

    Thanks,
    Ryan

  5. I have similar issue. In this case empty rows appear in top of DataGrid if number of DataGrids are instantiated. I created video to show this and put it to

    http://cid-69f11148404c9c0e.skydrive.live.com/self.aspx/.Public/emptyrows.avi

    How to fix this ?
    This was also discussed in http://forums.silverlight.net/forums/t/181553.aspx


Leave a Reply

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

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

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

Facebook photo

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

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.