ASP.NET Dynamically Adding a Gridview
<p>I have a gridview as such</p>

<pre><code>&lt;asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." AlternatingRowStyle-CssClass="tr_dark"  HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" AllowSorting="false" Width="100%" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand"&gt;

    &lt;Columns&gt;                         
            &lt;asp:TemplateField HeaderText="Product Name"  SortExpression="productName" HeaderStyle-CssClass="product" &gt;
                &lt;ItemTemplate&gt;   
                &lt;asp:Label ID="ProductNameField" runat="server" Text='&lt;%# Eval("description").ToString() %&gt;'&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;                     
            &lt;/asp:TemplateField&gt; 

    &lt;/Columns&gt;
    &lt;Columns&gt;                         
            &lt;asp:TemplateField HeaderText="Pack Size"  SortExpression="packSize" HeaderStyle-CssClass="packsize" &gt;
                &lt;ItemTemplate&gt;  
                &lt;asp:Label ID="PackSizeField" runat="server" Text='&lt;%#  Eval("packSize").ToString()%&gt;'&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;                     
            &lt;/asp:TemplateField&gt; 

    &lt;/Columns&gt;
    &lt;Columns&gt;                         
            &lt;asp:TemplateField HeaderText="Stock"  SortExpression="address" HeaderStyle-CssClass="stock"&gt;
                &lt;ItemTemplate&gt;   
                &lt;asp:Label ID="StockField" runat="server" Text='&lt;%#   DisplayStockLevel(Eval("StockIndicator").ToString()) %&gt;'&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;                     
            &lt;/asp:TemplateField&gt; 

    &lt;/Columns&gt;
    &lt;Columns&gt;
    &lt;asp:TemplateField HeaderText="Quantity" HeaderStyle-CssClass="quantity" &gt;
        &lt;ItemTemplate&gt;
            &lt;asp:TextBox runat="server"  Width="30" ID="txtQuantity" Text='&lt;%# Eval("Quantity") %&gt;'&gt;&lt;/asp:TextBox&gt;
            &lt;asp:TextBox runat="server"  Visible="false" Width="30" ID="txtProductCode" Text='&lt;%# Eval("ProductCode") %&gt;'&gt;&lt;/asp:TextBox&gt;
        &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;/Columns&gt;

    &lt;Columns&gt;
    &lt;asp:TemplateField HeaderText="Actual Price"  HeaderStyle-CssClass="actual" SortExpression="address"&gt;
                &lt;ItemTemplate&gt;   
                &lt;asp:Label ID="TradePriceField" runat="server" Text='&lt;%#  DisplayMoney(Eval("UnitPrice").ToString())%&gt;'&gt;&lt;/asp:Label&gt;
                &lt;asp:Label ID="TradePriceFieldHidden" runat="server" Text='&lt;%#  Eval("UnitPrice").ToString()%&gt;' Visible="false"&gt;&lt;/asp:Label&gt;
                &lt;/ItemTemplate&gt;                     
            &lt;/asp:TemplateField&gt;
    &lt;/Columns&gt;
    &lt;Columns&gt;
    &lt;asp:BoundField DataField="TotalPrice" HeaderText="Total" HeaderStyle-CssClass="total" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" /&gt;
    &lt;/Columns&gt;
    &lt;Columns&gt;
    &lt;asp:TemplateField HeaderText="" HeaderStyle-CssClass="remove"&gt;
    &lt;ItemTemplate&gt;
        &lt;asp:ImageButton  ImageUrl="~/img/icons/cross.gif"  width="10" height="10" alt="Cancel" runat="server" ID="btnRemove" CommandName="Remove" CommandArgument='&lt;%# Eval("ProductId") %&gt;'/&gt;
    &lt;/ItemTemplate&gt;
    &lt;/asp:TemplateField&gt;
    &lt;/Columns&gt;
</code></pre>

<p></p>

<p>And now trying to add a footer rows dynamically ( because I need multiple footer rows)</p>

<pre><code> Protected Sub gvShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.RowDataBound
    ' If we are binding the footer row, let's add in our total
    If e.Row.RowType = DataControlRowType.Footer Then
        e.Row.Cells(5).Text = "&lt;strong&gt;Total Cost:&lt;/strong&gt;"
        e.Row.Cells(6).Text = ShoppingCart.Instance.GetSubTotal().ToString("C")
    End If

    Dim grid As GridView = CType(sender, GridView)

    ''gets the current footer row to clone
    Dim footer As GridViewRow = grid.FooterRow
    Dim numCells = footer.Cells.Count

    Dim newRow As New GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState)

    ''have to add in the right number of cells
    ''this also copies any styles over from the original footer
    For i As Integer = 0 To numCells - 1
        Dim emptyCell As New TableCell
        emptyCell.ApplyStyle(grid.Columns(i).ItemStyle)

        newRow.Cells.Add(emptyCell)
    Next

    newRow.Cells(5).Text = "Total Discount:"
    newRow.Cells(6).Text = "55.00"

    ''add new row to the gridview table, at the very bottom
    CType(grid.Controls(0), Table).Rows.Add(newRow)

End Sub
</code></pre>

<p>But getting the error</p>

<p>Object reference not set to an instance of an object.</p>

<p>Dim numCells = footer.Cells.Count</p>

<p>Any idea whats going wrong ?</p>
