Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update single row in a WPF DataGrid

Tags:

c#

wpf

datagrid

I'm creating a download manager, and my WPF DataGrid is bound to a collection of objects representing ongoing downloads (in separate threads). When I have multiple downloads running, each one is using this code to update its DataGrid item every second:

if (DateTime.Now > download.LastUpdateTime.AddSeconds(1))
{
    this.downloadsGrid.Items.Refresh();
    download.LastUpdateTime = DateTime.Now;
}

Datagrid.Items.Refresh() does the job, but it reconstructs the whole DataGrid , causing all downloads to update each others DataGrid rows several times in one second, and I don't want that kind of behavior. Is there any way to refresh a specific row (item) in a DataGrid?

like image 668
marko Avatar asked Dec 03 '22 03:12

marko


1 Answers

If you bind your DataGrid to an ObservableCollection (which implements INotifyCollectionChanged) your DataGrid will be notified when a new item is added or an item is remove. Additionally, if you're just updating a property on an object in the collection the object should implement INotifyPropertyChanged and raise the PropertyChanged event which will tell the DataGrid to just update that value.

like image 52
Dan Busha Avatar answered Dec 25 '22 04:12

Dan Busha