klutch1991
klutch1991

Reputation: 239

Loading data to datagrid takes too much time

I have a DataGrid control in my WPF app. It's bound to ObservableCollection of Model objects. Each Model object has a constructor, which applies an EF-Model object as an argument. After app user enters correct credentials, there's a method, which should be executed. This method load's data asynchonously from DB (about 1000 EF-Model objects). After loading I pass each object to Model constructor (smth like MyEFModelObjectsCollection.ForEach(o => MyObservableCollection.Add(new Model(o)))). But it takes too long time to load this thousand of objects into my DataGrid. How should I deal with it?

Upvotes: 0

Views: 1332

Answers (3)

paparazzo
paparazzo

Reputation: 45106

Use ListView / GridView unless you need to edit
If you only need basic editing (Textbox) still use ListView

DataGrid does a lot but it comes with a LOT of overhead
The data model alone is way complex

Upvotes: 2

Dean Chalk
Dean Chalk

Reputation: 20471

One good way of achieving a better user experience in this type of scenario is to have a wrapper ViewModel for each row. Create 1000 wrappers and bind your grid to the collection of these new ViewModels. On your DataGrid Columns XAML use the FallBack value in the bindings to show a temporary value. Then when your asynchronous data service returns your Model objects, pass each one into a wrapper ViewModel and update the ViewModel's properties with the new values, so that the grid is allways responsive, and gradually shows the correct data.

Upvotes: 1

Anton
Anton

Reputation: 748

Possible reason is that the method ObservableCollection.Add() generate event CollectionChanged each time. So it is impact on perfomance of your application. Furthermore ObserverCollection not contains method AddRange(). But you can add own ObservableCollectionEx:ObservableCollection and implement AddRange(IEnumerable collection) method. You can find many examples how to do it.

Upvotes: 1

Related Questions