WPF Datagrid not refreshing UI

This is another annoying thing i have come accross. Sometimes we have a need to filter a datagrid which is already filled. Now, this problem is only for WPF datagrids. Even after trying DataGrid. Items.Referesh(), the UI won’t udpate.

The reason for this is that DataGrid.ItemSource doesn’t get notified when the underlying datasource changes. Hence, to refersh the UI, you would need to reassign the DataConext to DataGrid. One way to do this is to re-instantiate the datasource and then re-assign the datacontext. So the steps would be

  1. Set DataContext to null
  2. Re-create/instantiate the datasource e.g. dataset
  3. Assign the dataset.Table[0] or whatever your item source is.

This solutiosn works for me assuming that most of the assignment is done within the code with minimum script in XAML, except the ItemSource property is set to {Binding} in XAML.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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