Symmetri Developer Blog

August 14, 2009

WPF DataGrid not refreshing?

General, .NET/C#, WPF/Silverlight - By Shourov Bhattacharya

This is an age-old problem, and the latest version of .NET 3.5/WPF has not solved it. I have been working with the WPF DataGrid that comes with the WPF toolkit, and I have found that refreshing the DataGrid’s DataContext was *not* updating the DataGrid’s display in the UI. I had to add a second line of code to call DataGrid.Items.Refresh() to get the DataGrid to update its display:

dg.DataContext = _items;
dg.Items.Refresh();

Simple really, and a problem that is common to other similar components on other platforms too. However, I am not sure I see the justification in not re-rendering the DataGrid when changing the data context automatically. I suppose there might be an argument for separating out the rendering process if it is a high cost operation and giving the developer control over it (which is what the DataGrid.Items.Refresh() method does).

However, I do not see that behaviour as having been properly documented; and in the absence of documentation, I think it is very reasonable for the developer to expect that the grid display is automatically refreshed when the underlying data is changed. The philosophy behind WPF seems to be that data binding should be more flexible and seamless; overall, it is, but I think having to do this kind of old-school “refresh”-ing detracts from that goal.

Comments »

No comments yet.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Janis Joseph