title: Add new row to gridview not working inside repeater control
body: <p>I'm using Gridview inside a repeater control to select items for billing. </p>
  
  <p>but when I try to add new row in the gridview it says:</p>
  
  <p>"Object reference not set to an instance of an object."</p>
  
  <p>I'm using gridview to show item details and have a button to add new row to the gridview.</p>
  
  <p>Below is my c# code to add new row:</p>
  
  <pre><code>private void AddNewRowToGrid()
      {
          foreach (RepeaterItem rptrItems in rptrTables.Items)
          {
              GridView gvItems = (GridView)rptrTables.FindControl("grdOrder");
              int rowIndex = 0;
              if (ViewState["CurrentTable"] != null)
              {
                  DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                  DataRow drCurrentRow = dtCurrentTable.NewRow();
                  if (dtCurrentTable.Rows.Count &gt; 0)
                  {
                      for (int i = 1; i &lt;= dtCurrentTable.Rows.Count; i++)
                      {
                          DropDownList box1 = (DropDownList)gvItems.Rows[rowIndex].Cells[1].FindControl("drpItemname");
                          TextBox box2 = (TextBox)gvItems.Rows[rowIndex].Cells[2].FindControl("lblUnitPrice");
                          TextBox box3 = (TextBox)gvItems.Rows[rowIndex].Cells[3].FindControl("lblQuantity");
                          TextBox box4 = (TextBox)gvItems.Rows[rowIndex].Cells[4].FindControl("lblLineTotal");
                          drCurrentRow = dtCurrentTable.NewRow();
                          drCurrentRow["RowNumber"] = i + 1;
                          dtCurrentTable.Rows[i - 1]["ItemName"] = box1.Text;
                          dtCurrentTable.Rows[i - 1]["UnitPrice"] = box2.Text;
                          dtCurrentTable.Rows[i - 1]["Quantity"] = box3.Text;
                          dtCurrentTable.Rows[i - 1]["LineTotal"] = box4.Text;
                          rowIndex++;
                      }
                      dtCurrentTable.Rows.Add(drCurrentRow);
                      ViewState["CurrentTable"] = dtCurrentTable;
                      gvItems.DataSource = dtCurrentTable;
                      gvItems.DataBind();
                  }
              }
              else
              {
                  Response.Write("ViewState is null");
              }
              SetPreviousData();
          }        
      }
  </code></pre>
  
  <p>And here is my aspx Markup:</p>
  
  <pre><code>&lt;div class="card-body"&gt;
                                                      &lt;div class="row"&gt;
                                                          &lt;div class="col-md-12"&gt;
                                                              &lt;div class="form-group"&gt;
                                                                  &lt;asp:GridView ID="grdOrder" runat="server" AutoGenerateColumns="false" AllowPaging="true" CssClass="table table-bordered table-hover table-responsive" GridLines="None" PageSize="10" OnRowDataBound="gvRowDataBound" ShowFooter="true"&gt;
                                              &lt;Columns&gt;
                                                   &lt;asp:CommandField ShowDeleteButton="true" ControlStyle-CssClass="btn btn-danger fa fa-trash" DeleteText="" HeaderText="Remove" /&gt;
                                                  &lt;asp:BoundField DataField="RowNumber" HeaderText="Sl. No." /&gt;
                                                  &lt;asp:TemplateField HeaderText="Item Name"&gt;
                                                      &lt;ItemTemplate&gt;
                                                          &lt;asp:DropDownList ID="drpItemname" runat="server" CssClass="form-control select2" OnSelectedIndexChanged="GetItemDetails" AutoPostBack="true"&gt;&lt;/asp:DropDownList&gt;
                                                      &lt;/ItemTemplate&gt;
                                                  &lt;/asp:TemplateField&gt;
                                                  &lt;asp:TemplateField HeaderText="Price"&gt;
                                                      &lt;ItemTemplate&gt;
                                                          &lt;asp:TextBox ID="lblUnitPrice" runat="server" CssClass="form-control" Text='&lt;%# Eval("UnitPrice")%&gt;'&gt;&lt;/asp:TextBox&gt;
                                                      &lt;/ItemTemplate&gt;
                                                  &lt;/asp:TemplateField&gt;
                                                  &lt;asp:TemplateField HeaderText="Qty"&gt;
                                                      &lt;ItemTemplate&gt;
                                                          &lt;asp:TextBox ID="lblQuantity" runat="server" TextMode="Number" CssClass="form-control" Text='&lt;%# Eval("Quantity")%&gt;'&gt;&lt;/asp:TextBox&gt;
                                                      &lt;/ItemTemplate&gt;
                                                  &lt;/asp:TemplateField&gt;                                               
                                                  &lt;asp:TemplateField HeaderText="Line Total"&gt;
                                                      &lt;ItemTemplate&gt;
                                                          &lt;asp:TextBox ID="lblLineTotal" runat="server" CssClass="form-control" Text="0"&gt;&lt;/asp:TextBox&gt;
                                                      &lt;/ItemTemplate&gt;
                                                      &lt;FooterStyle HorizontalAlign="Right" /&gt;
                                                  &lt;FooterTemplate&gt;
                                                      &lt;asp:Button ID="ButtonAdd" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="AddItem" CausesValidation="False" /&gt;
                                                  &lt;/FooterTemplate&gt;                                                   
                                                  &lt;/asp:TemplateField&gt;                                        
                                              &lt;/Columns&gt;
                                          &lt;/asp:GridView&gt;
                                                              &lt;/div&gt;
                                                          &lt;/div&gt;
                                                      &lt;/div&gt;
                                                  &lt;/div&gt;
                                                  &lt;div class="card-footer"&gt;
                                                      &lt;div class="pull-right"&gt;
                                  &lt;asp:Button ID="btnSubmit" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print KOT" /&gt;
                                  &lt;asp:Button ID="Button1" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print Final Bill" /&gt;
                                  &lt;asp:Button ID="Button2" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Complete Order" /&gt;
                              &lt;/div&gt;
                                                  &lt;/div&gt;
  </code></pre>
  
  <p>I want to add new row to the gridview provided all the data remains intact in the previous row!</p>