Only get the data that is needs to be displayed

I have been on the look out for the ability to display and search a customers database, but having the ability to display millions of records if that what the end users wants.  But the trouble is that it can take a long time to return a million records from a data source.

I’ve talked about paging before in my article LINQ-to-SQL-and-Paging, which works fine for internet based paging, but what about WPF and WinForm Grids, well I came across Vitrual Mode for the Microsoft Data Grid, which can provide an answer.  What this allow you to do is, load the data you can see on the screen very quickly and as you scroll through the data is fetched when required.

By setting the VirtualMode = true for a data grid, it allows you to add a handler for deail with CellValueNeeded 

Here is the main body of the code

Each time the Data grid needs some values it will call the following:

And of course the NameListCache class


I have attached the source, you will also find a PopulateData project to populate the database with data. (19.43 kb) (11.43 kb)

Here are a few of the database scripts that yu will need for the sample

Names.sql (581.00 bytes)

GetNames.sql (786.00 bytes)