I have a WPF datagrid, bound to a list populated by linq-to-sql from a database. The binding is two-Way, allowing the user to change the values in each row
<wpf:DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding MyList}"
SelectedItem="{Binding SelectedItem}" >
When displaying about 20000 rows, the program crashes with an out-of-memory exception during initialisation of the list. Performance becomes unbearably slow even with less rows.
I know that on initialization the datagrid iterates through each row to measure the maximum column width and other properties. It will apparently do so for all rows regardeless of whether they are on screen.
I tried either binding the datagrid to myQuery.ToList()
(to allow sorting the datagrid by clicking on the columns) or binding directly to the IQueryable. (Sorting does not work with that)
Both produce the same result. The ToList() with 20000 items alone does not cause the massive memory consumption, this happens only when it is bound to the datagrid .
Ignoring the issue of how useful 20000 rows in a datagrid are (those are the current requirements; to change those a working example would be helpful).
What is the most simple way to lazily load only the data currently shown on the screen, and ignore everything else until it is scrolled into view?
Can this be done without third party libraries and major code changes?
If not, what would be the recommended workaround?