Jefim
Jefim

Reputation: 3077

Displaying a large collection in a DataGrid

The amount of items in collection: ~100k The amount of field displayed in columns: 4-10

The problem itself - the collection is taken from a database using EntityFramework. It takes about 10-12s on dev computers to load and materialize all the required data. Yet another thing that comes up is that the same collection can be bound to several controls, and, therefore, they must be filtered separately (= not setting the default collection view filters). Currently, I set the binding as follows:

Binding b = new Binding();
b.Source = new CollectionViewSource() { Source = MyLargeCollection }.View;
MyDataGrid.SetBinding(DataGrid.ItemsSourceProperty, b);

Creating a new CollectionViewSource greatly increases the time it takes to initialize - several minutes (and I suspect that it is enumerating the 100k collection for some reason). I mean that if I just set:

b.Source = MyLargeCollection;

It will just take those 10-12 seconds to load and materialize data from the database.

The question - is there some problem with my code? If not - what would be the right approach for binding the same large collection to different items controls but with different collection views?

Upvotes: 0

Views: 653

Answers (1)

Wouter Janssens
Wouter Janssens

Reputation: 1613

Just use Linq to Entities to load the entities with its specified filter to make sure you don't load all 10k records because no user is interested in a grid with 10k records.

Example of binding to a query:

grid1.DataContext = (from i in context.MyItems
                    where i.MyPropertyToFilter == "myFilter"
                    select i).ToList();

grid2.DataContext = (from i in context.MyItems
                    where i.MyOtherPropertyToFilter == "myOhterFilter"
                    select i).ToList();

In this way you only load the records needed for your controls

Upvotes: 1

Related Questions