title: C# DataGridView Overflow Error
body: <p>I am trying to enter data into my DataGridView table from a database. The problem I am having is that I am only able to retrieve and set the first row of data from the database before getting an overflow error.<br>
  This is my code: </p>
  
  <pre><code>string passedvalue = "Tops"
  DataSet ds = new DataSet();
  OleDbDataAdapter da = new OleDbDataAdapter();
  DataTable dt = new DataTable();
  
  string command = "SELECT ProductName,Price,Stock FROM Products WHERE Category = \"" + passedvalue + "\"";
  da = new OleDbDataAdapter(command, con);
  
  da.Fill(ds, "TempTable");
  dt = ds.Tables["TempTable"];
  
  
  int count = 0;
  
  foreach (DataRow row in dt.Rows)
  {
      try
      {
          dgProducts.Rows[count].Cells[0].Value = row["ProductName"].ToString();
          dgProducts.Rows[count].Cells[1].Value = row["Price"].ToString();
          dgProducts.Rows[count].Cells[2].Value = row["Stock"].ToString();
      }
      catch (Exception) { }
      count++;
  }
  </code></pre>
  
  <p>My database is as follows:  </p>
  
  <pre><code>ID,ProductName,Price,Stock,Category
  0, blue top, 5, 10, Tops
  1, green top, 6, 55, Tops
  2, blue trousers, 4, 4, Trousers
  3, red top, 5, 5, Tops  
  </code></pre>
  
  <p>When I run the code,</p>
  
  <pre><code>blue tops, 5, 10
  </code></pre>
  
  <p>is added into the table before the error is thrown.  </p>
  
  <p>I believe that I have included any required information, but if not, please comment it below and I'll add it.   </p>
  
  <p><strong><em>EDIT:</em></strong><br>
  dgProducts is a DataGridView  </p>
  
  <pre><code>this.dgProducts = new System.Windows.Forms.DataGridView();  
  </code></pre>
  
  <p>In the form designer  </p>
  
  <p>The exception is:</p>
  
  <pre><code>    System.ArgumentOutOfRangeException was unhandled
    HResult=-2146233086
    Message=Index was out of range. Must be non-negative and less than the size of the collection.
  Parameter name: index
    ParamName=index
    Source=mscorlib
    StackTrace:
         at System.Collections.ArrayList.get_Item(Int32 index)
         at System.Windows.Forms.DataGridViewRowCollection.SharedRow(Int32 rowIndex)
         at System.Windows.Forms.DataGridViewRowCollection.get_Item(Int32 index)
         at program.productSelect.productSelect_Load(Object sender, EventArgs e) in \\--\--\Home\start2016\--\Visual Studio 2015\Projects\--\--\productSelect.cs:line 54
         at System.Windows.Forms.Form.OnLoad(EventArgs e)
         at System.Windows.Forms.Form.OnCreateControl()
         at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
         at System.Windows.Forms.Control.CreateControl()
         at System.Windows.Forms.Control.WmShowWindow(Message&amp; m)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
         at System.Windows.Forms.Form.WmShowWindow(Message&amp; m)
         at System.Windows.Forms.Form.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    InnerException:
  </code></pre>