title: DataGrid row request patterns with data virtualization
body: <p>I implemented a data virtualization solution using some ideas from <a href="http://datavirtualization.codeplex.com" rel="nofollow noreferrer">CodePlex</a> and the blog of <a href="http://web.archive.org/web/20150722011154/http://www.zagstudio.com:80/blog/498" rel="nofollow noreferrer">Bea Stollnitz</a> and Vincent Da Ven Berhge's paper (same link). However I needed a different approach so I decided to write my own solution.</p>
  
  <p>I am using a <code>DataGrid</code> to display about a million rows with this solution. I am using UI virtualization as well. My solution is feasible, but I experience some weird behavior in certain situations on how the <code>DataGrid</code> requests data from its source.</p>
  
  <p>About the solution</p>
  
  <p>I ended up writing a list which does all the heavy work. It is a generic class named <code>VirtualList&lt;T&gt;.</code> It implements the <code>ICollectionViewFactory</code> interface, so the collection view creation mechanism can create a <code>VirtualListCollectionView&lt;T&gt;</code> instance to wrap it. This class inherits from <code>ListCollectionView</code>. I did not follow the suggestions to write my own <code>ICollectionView</code> implementation. Inheriting seems to work fine as well.</p>
  
  <p>The <code>VirtualList&lt;T&gt;</code> splits the whole data into pages. It gets the total item count and every time the <code>DataGrid</code> requests for a row via the list indexer it loads the appropriate page or returns it from the cache. The pages are recycled inside and a <code>DispatcherTimer</code> disposes unused pages in idle time.</p>
  
  <p>Data request patterns</p>
  
  <ul>
  <li><p>The first thing I learned, that <code>VirtualList&lt;T&gt;</code> should implement <code>IList</code> (non generic). Otherwise the <code>ItemsControl</code> will treat it as an <code>IEnumerable</code> and query/enumerate all the rows. This is logical, since the <code>DataGrid</code> is not type safe, so it cannot use the <code>IList&lt;T&gt;</code> interface.</p></li>
  <li><p>The row with 0 index is frequently asked by the <code>DataGrid</code>. It is seem to be used for visual item measurement (according to the call stack). So, I simply cache this one.</p></li>
  <li><p>The caching mechanism inside the <code>DataGrid</code> uses a predictable pattern to query the rows it shows. First it asks for the visible rows from top to bottom (two times for every row), then it queries a couple of rows (depending on the size of the visible area) before the visible area (including the first visible row) in a descending order so, from bottom to top. After that it requests for a same amount of rows after the visible rows (including the last visible row) from top to bottom.</p>
  
  <p>If the visible row indexes are 4,5,6. The data request would be: 4,4,5,5,6,6,4,3,2,1,6,7,8,9.</p>
  
  <p>If my page size is properly set, I can serve all these requests from the current and previously loaded page.</p></li>
  <li><p>If <code>CanSelectMultipleItems</code> is <code>True</code> and the user selects multiple items using the SHIFT button or mouse drag, the <code>DataGrid</code> enumerates all the rows from the beginning of the list to the end of the selection. This enumeration happens via the <code>IEnumerable</code> interface regardless of that <code>IList</code> is implemented or not.</p></li>
  <li><p>If the selected row is not visible and the current visible area is "far" from the selected row, sometimes DataGrid starts requesting all the items, from the selected row to the end of the visible area. Including all the rows in between which are not even visible. I could not figure out the exact pattern of this behavior. Maybe my implementation is the reason for that.</p></li>
  </ul>
  
  <p>My questions</p>
  
  <ul>
  <li><p>I am wondering, why the <code>DataGrid</code> requests for non visible rows, since those rows will be requested again when become visible? </p></li>
  <li><p>Why is it necessary to request every row two or three times?</p></li>
  <li><p>Can anyone tell me how to make the DataGrid not to use <code>IEnumerable</code>, except turning off multiple item selection?</p></li>
  </ul>