Tuesday, December 9, 2008

Data Grid Disappearing Line Trick/Bug

I've been working with the Silverlight Datagrid for a while now. Nifty control.  But it has caused me a great deal of frustration! One bug I have run across (on the 2.0 bits) is the occasional disappearing of rows. After a lot of debugging, yelling at my computer, and many, many Google searches, I was able to figure it out.

It seems this problem occurs whenever a control inside the datagrid has focus and the ItemsSource is modified. I have seen this when I add, update or delete items in the ItemsSource. Ahhh! Very annoying bug! The easiest work around.... make sure no controls inside the datagrid have focus prior to messing with the ItemsSource. In my application, I have several checkboxes and buttons in every row of the datagrid. In the event handlers for the click events or checked/unchecked events, I simply set focus to the entire datagrid (myGrid.Focus()), before I run any of the code that modifies that row. That seemed to take care of the situation.

From other posts I have read out there, a different way to do this is to remove the datagrid from the visual tree and replace it every time you modify the ItemsSource... but that seems a bit drastic. Hopefully this post will become obsolete very soon. (Hint hint. Microsoft... Please fix this soon!)

3 comments:

  1. Hello. It looks like you are know a lot about this problem. 4 months after Microsoft post fix to this bug, I experience very similar issue. I'm working with CSLA Light,when I remove child object, delete line from grid, column which fire code that delete data is completely clean. Another 2 columns in selected row are 50% clean, and last column Hyperlinkbutton is still there. When I save and reopen page, this line is gone. Any suggestion what can cause this behavior?
    It is still possible that I have problem with dll's. When I check properties for System.Windows.Controls.Data, it show me that I reference version 2.0.5.0. Is it new version?

    Thanks for any suggestion.

    ReplyDelete
  2. I'm not really sure what could be happening there. That is the right version of the dll (2.0.5.0). Maybe Silverlight 3 will take care of the problem for you.

    ReplyDelete
  3. Thanks for your response

    ReplyDelete