简体   繁体   中英

Slow WPF 4 Datagrid Refresh

I am using the WPF DataGrid component from the .NET 4 framework, it is bound to a thread safe observable collection which I found here : http://www.deanchalk.me.uk/post/Thread-Safe-Dispatcher-Safe-Observable-Collection-for-WPF.aspx

The program is a System Admin tool which pings each IP Address in a range, if there is a response it creates an object in the collection with some details from the computer.

The problem I am having, is poor performance. Initially, I was just updating the Collection and letting the datagrid pick up the changes. This caused an issue where the DataGrid control wasn't refreshing and showing the data unless I scrolled.

So I added a timer to call the grids refresh method, the timer ticks every 750ms. This worked brilliantly, until I realised that the programs UI is completely unresponsive while the timer is enabled and it is scanning.

Without the timer, performance is acceptable, without it, its terrible. I have tried several values for the timeout period (up to 2000ms) without any luck and have also made sure that my columns are fixed width (I read that autogenerated columns and widths can cause performance issues).

The amount of Row's in the grid is about 300 - 400 with 5 columns, so its not exactly a huge grid.

Does anyone have any suggestions of how I could get the grid to update in a timely manner when the collection behind is added to, without sacrificing performance?

If your bindings are correct you won't need to refresh the DataGrid manually. Updating the observable collection behind the scene should automatically trigger(assuming you have implemented NotifyPropertyChanged) the update of DataGrid. 300 - 400 is not a number that will slow down the DataGrid. Is your DataGrid inside ScrollViewer? sometimes that degrades the performance as well. Make sure your bindings are working fine then you won't need manual refresh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM