A Programmer's Dream

WPF Datagrid Items Refresh

Posted by Stephen Wrighton on 07 Jan 2009

So, I have this WPF datagrid, and it's quite happy in the way it works. With a single exception. Some background, I'm not doing the base behavior for editing/adding, but rather have a separate control outside the grid which is responsible for that behavior (as it is also the control responsible for adding the initial element).

But I was having a problem, and it was with the Grid's UI.

My changes and additions were not making their way into the UI display. No matter how many times I invalidated the User Interface it just would not get displayed. And this was not just for Updates, but also for Inserts and Deletes.

VERY much annoying.

But, I wouldn't be here if I didn't have a solution.

As stated, my initial try was to use the Datagrid.InvalidateUI command, as that's basically what I was trying to do: repaint the grid. What I wasn't doing with that was telling the Datagrid to look at the underlying datasource and refresh the UI from that.

The magic command? Datagrid.Items.Refresh()

Yeah, I did feel a bit silly after finding that out. Of course I blame it on the fact that I've spent too much time in the ASP.NET arena where I can force a data-rebinding by calling the DataBind method of the object. Oh well, I solved the problem, so I'm all sorts of happy.

Tweet me @kidananubix if you like this post.