I have a Datagrid connected to Datatable, which needs to load a very large amount of rows.
To speed things up, I load 10% of the rows, and display the form. Most of the time the user only needs those 10% (they are the most recent entries). In a background thread I load the remaining 90% of the rows into another datatable (SecondData). Then I merge both datatables:
FirstData.BeginLoadData()
FirstData.Merge(SecondData, False)
FirstData.EndLoadData()
This works fine, but the Merge operation takes a very long time. If I reverse the operation (merging SecondData with FirstData), it takes much less time. But then I have to re-assign an itemsource (SecondData) to the Datagrid, and the user looses the current scrolling position, selected row, etc.
I also tried adding the rows directly to FirstData from the background thread, and it appears to work just fine. But when I scroll the Datagrid after that, I get freezes, and “DataTable internal index is corrupted”, after that.
What would be the correct way of doing this?
Here is a somewhat hacked additional version that shows how to load a DataGrid when binding to a DataView using still BeginInvoke.
The code still loads one row at a time into the DataGrid.
You’ll need to modify as needed; I am loading from the AdventureWorks sample using the Loaded event.
Here is how the ViewModel works:
Here is the window:
Here is the Window code-behind with the Loaded event:
Here the ViewModel: