title: "Specified argument was out of the range of valid values"
body: <p>I'm adding dynamic Rows in ASP.Net GridView Control with TextBoxes. But I'm getting the error below when I click on my <strong>Add New Row</strong> button.</p>
  
  <blockquote>
    <p>Specified argument was out of the range of valid values. Parameter name: index</p>
  </blockquote>
  
  <p>What could be the error ?</p>
  
  <p><strong>Th code in my <em>.aspx</em> file</strong></p>
  
  <pre><code>&lt;div id="Div1" class="divTable"&gt;
      &lt;asp:gridview id="Gridview1" runat="server" autogeneratecolumns="false" gridlines="None"
          width="100%" height="100%"&gt;
              &lt;Columns&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;ItemTemplate&gt;
                          &lt;div class="divRow"&gt;
                              &lt;div class="divColumn"&gt;
                                  &lt;div&gt;
                                      &lt;asp:Label ID="lbl1" runat="server" Text="Type Name" CssClass="formLable"&gt;&lt;/asp:Label&gt;
                                  &lt;/div&gt;
                                  &lt;div&gt;
                                      &lt;asp:TextBox ID="txt_type" Width="200" runat="server" CssClass="txtbox"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/div&gt;
                                  &lt;span&gt;
                                      &lt;asp:RequiredFieldValidator ID="TYPE_NAME_VAL" runat="server" ControlToValidate="txt_type"
                                          ErrorMessage="Type is required." Display="Dynamic" CssClass="error"&gt;&lt;/asp:RequiredFieldValidator&gt;
                                  &lt;/span&gt;
                              &lt;/div&gt;
                              &lt;div class="divColumn"&gt;
                                  &lt;div&gt;
                                      &lt;asp:Label ID="lbl2" runat="server" Text="Total" CssClass="formLable"&gt;&lt;/asp:Label&gt;
                                  &lt;/div&gt;
                                  &lt;div&gt;
                                      &lt;asp:TextBox ID="txt_total" Width="200" runat="server" CssClass="txtbox"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="divRow"&gt;
                              &lt;div class="divColumn"&gt;
                                  &lt;div&gt;
                                      &lt;asp:Label ID="lbl3" runat="server" Text="Max" CssClass="formLable"&gt;&lt;/asp:Label&gt;
                                  &lt;/div&gt;
                                  &lt;div&gt;
                                      &lt;asp:TextBox ID="txt_max" Width="200" runat="server" CssClass="txtbox"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="divColumn"&gt;
                                  &lt;div&gt;
                                      &lt;asp:Label ID="lbl4" runat="server" Text="Min" CssClass="formLable"&gt;&lt;/asp:Label&gt;
                                  &lt;/div&gt;
                                  &lt;div&gt;
                                      &lt;asp:TextBox ID="txt_min" Width="200" runat="server" CssClass="txtbox"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class="divColumn"&gt;
                                  &lt;div&gt;
                                      &lt;asp:Label ID="lbl5" runat="server" Text="Rate" CssClass="formLable"&gt;&lt;/asp:Label&gt;
                                  &lt;/div&gt;
                                  &lt;div&gt;
                                      &lt;asp:TextBox ID="txt_rate" Width="200" runat="server" CssClass="txtbox"&gt;&lt;/asp:TextBox&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"
                              CausesValidation="False" /&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
          &lt;/asp:gridview&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>C# code of ButtonAdd_Click()</strong></p>
  
  <pre><code>if (ViewState["CurrentTable"] != null)
  {
      DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
      DataRow drCurrentRow = null;
      if (dtCurrentTable.Rows.Count &gt; 0)
      {
          for (int i = 1; i &lt;= dtCurrentTable.Rows.Count; i++)
          {
              //extract the TextBox values
              TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_type");
              TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_total");
              TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_max");
              TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_min");
              TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_rate");
  
              drCurrentRow = dtCurrentTable.NewRow();
  
              dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
              dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
              dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
              dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
              dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;
  
              rowIndex++;
          }
          dtCurrentTable.Rows.Add(drCurrentRow);
          ViewState["CurrentTable"] = dtCurrentTable;
  
          Gridview1.DataSource = dtCurrentTable;
          Gridview1.DataBind();
      }
  }
  else
  {
      Response.Write("ViewState is null");
  }
  </code></pre>