title: DataGridView virtual mode with a simple list as a source
body: <p>Previously I asked a question about my dataGridView's performance due to it havign to display a large amount of rows that get added based on an incoming stream. Multiple solutions were given, one of them enabling virtual mode. MSDN has an article on the subject but it feels more complicated than what I need as it uses a database and an editable field. My DataGridView is only for displaying and the data I display is placed in a List.</p>
  
  <p>After I accepted an answer I received this link: <a href="http://www.codeproject.com/Articles/23937/Paging-Data-with-DataGridView-in-VirtualMode" rel="noreferrer">http://www.codeproject.com/Articles/23937/Paging-Data-with-DataGridView-in-VirtualMode</a> . Even though that uses a database example, it's better suited for what I need. My List that will contain the data I want to display is declared as follows:</p>
  
  <pre><code>List&lt;ResultRow&gt; captureResults = new List&lt;ResultRow&gt;();
  </code></pre>
  
  <p>A ResultRow object is defined as follows:</p>
  
  <pre><code>/* Simplified */
  public class ResultRow
  {
      private int first = 0;
      private string second = "";
      private UInt64 third = 0;
      private IPAddress fourth = null;
      /* etc */
  
      public ResultRow()
      {
      }
  
      public void Set (&lt;the values&gt;) //In actuallity a KeyValuePair
      {
          //field gets set here
      }
  
      public UInt64 Third
      {
          get { return third; }
          set { third = value; }
      }
  
      /* etc. */
  </code></pre>
  
  <p>}</p>
  
  <p>Following the article mentioned above, i created a ResultRowCache. The object is made as follows:</p>
  
  <pre><code>/* Page size set to 100. */
  ResultRowCache _cache   = new ResultRowCache(PAGE_SIZE, captureResults);
  </code></pre>
  
  <p>On my form Load event I do the following (related to this problem. I also added an event handler although that's done using the IDE so not directly showing in this code. Definition below!)):</p>
  
  <pre><code>dataGrid.VirtualMode = true;
  
  _cache = new ResultRowCache(PAGE_SIZE, captureResults);
  
  dataGrid.Columns.Add("FirstColumn"  , "First column header");
  dataGrid.Columns.Add("Second Column", "Second column header");
  /* Etc. Adding all columns. (Every member or ResultRow has it's own column. */
  
  dataGrid.RowCount = (int)_cache.TotalCount;
  </code></pre>
  
  <p>One thing I am wondering is how the RowCount is initialized here. It's probably 0 (due to the constructor call of ResultRowCache (see below)) but it never seems to get changed again. Does this assignment count as a reference? How does itupdate itself?</p>
  
  <p>Anyway, onward with what I have, the ResultRowCache is defined as follows:</p>
  
  <pre><code>public class ResultRowCache
  {
      public int  PageSize    = 100;
      public long TotalCount;
      public List&lt;ResultRow&gt; CachedData = null;
      private List&lt;ResultRow&gt; FullData;
  
      int _lastRowIndex = -1;
  
      public ResultRowCache (int pageSize, List&lt;ResultRow&gt; total)
      {
          PageSize = pageSize;
          FullData = total;
  
          LoadPage( 0 );
      }
  
      public void LoadPage (int rowIndex)
      {
           int lastRowIndex = rowIndex - ( rowIndex % PageSize );
  
           /* Page already loaded */
           if( lastRowIndex == _lastRowIndex ) return;
  
           /* New page */
           _lastRowIndex = lastRowIndex;
  
           /* Create a new cashes data object */
           if( CachedData == null ) CachedData = new List&lt;ResultRow&gt;();
  
           /* If cached data already existed, clear */
           CachedData.Clear();
  
           /* The index is valid (there is data */
           if (lastRowIndex &lt; FullData.Count)
           {
               /* Not a full page */
               if (lastRowIndex + PageSize &gt; FullData.Count)
               {
                   CachedData = FullData.GetRange(lastRowIndex, ((lastRowIndex + PageSize) - 1) - FullData.Count);
  
               }
              /* Full page */
              else
              {
                  CachedData = FullData.GetRange(lastRowIndex, PageSize);
              }
          }
  
          TotalCount = CachedData.Count;
      }
      }
  }
  </code></pre>
  
  <p>Lastly, my CellValueNeeded event for the datagrid is defined as follows:</p>
  
  <pre><code>void DataGridCellValueNeededEvent(object sender, DataGridViewCellValueEventArgs e)
  {
      _cache.LoadPage(e.RowIndex);
  
      int rowIndex = e.RowIndex % _cache.PageSize;
  
      switch (dataGrid.Columns[e.ColumnIndex].Name)
      {
          /* Not actual names, example */
      case "FirstColumn":   e.Value = _cache.CachedData[rowIndex].First;  break;
          case "SecondColumn":  e.Value = _cache.CachedData[rowIndex].Second; break;
          /* Rest of the possibly columns/ResultRow values */
      }
  }
  </code></pre>
  
  <p>The problem: My datagrid remains empty even though the "captureResults" list gets filled. Here's what I tried so far:</p>
  
  <ul>
  <li>Update the RowCount member of the datagrid after the switch in the event.</li>
  <li>Declare the list with the total amount of results within the cache to ensure it's always up to date. (I was afraid "outside modifications" wouldn't go through to the List i passed in the cached constructor even though it was a reference. (Fairly new with C#))</li>
  <li>Set the RowCount of the datagrid to 100 (hard value) in the form's load event.</li>
  <li>Added an "Update()" call to the datagrid after adding something to the captureResults list. (this happens from a special thread which Invoke's a function that adds something to the list)</li>
  </ul>
  
  <p>None of the above changed anything. The grid remains empty. I think I am missing something quite obvious here. Any suggestions?</p>
  
  <p>-edit- Added something I tried to make it work. </p>