ASP.NET动态添加Gridview

时间:2011-08-17 09:22:21

标签: asp.net

我有这样的网格视图

<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">

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

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

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

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

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

现在尝试动态添加页脚行(因为我需要多个页脚行)

 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 = "<strong>Total Cost:</strong>"
        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

但是得到了错误

对象引用未设置为对象的实例。

Dim numCells = footer.Cells.Count

任何想法都会出错?

2 个答案:

答案 0 :(得分:0)

首先,您的GridView标记不正确。像这样定义Columns一次。

<asp:GridView ID="gvShoppingCart" runat="server" 
        AlternatingRowStyle-CssClass="tr_dark" 
        AllowPaging="true" 
        AllowSorting="false"
        AutoGenerateColumns="false"
        BorderWidth="0px" 
        DataKeyNames="ProductId"
        EmptyDataText="There is nothing in your shopping cart." 
        GridLines="None" 
        HeaderStyle-CssClass="header_req" 
        PageSize="25"  
        ShowFooter="true" 
        Width="100%"
        OnRowCommand="gvShoppingCart_RowCommand"
        OnRowDataBound="gvShoppingCart_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Product Name" SortExpression="productName" HeaderStyle-CssClass="product">
            <ItemTemplate>
                <asp:Label ID="ProductNameField" runat="server" 
                        Text='<%# Eval("description").ToString() %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Pack Size" SortExpression="packSize" HeaderStyle-CssClass="packsize">
            <ItemTemplate>
                <asp:Label ID="PackSizeField" runat="server" 
                        Text='<%#  Eval("packSize").ToString()%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Stock" SortExpression="address" HeaderStyle-CssClass="stock">
            <ItemTemplate>
                <asp:Label ID="StockField" runat="server" 
                        Text='<%# DisplayStockLevel(Eval("StockIndicator").ToString()) %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity" HeaderStyle-CssClass="quantity">
            <ItemTemplate>
                <asp:TextBox ID="txtQuantity" runat="server" 
                        Width="30" 
                        Text='<%# Eval("Quantity") %>'>
                </asp:TextBox>
                <asp:TextBox ID="txtProductCode" runat="server" 
                        Visible="false" 
                        Width="30" 
                        Text='<%# Eval("ProductCode") %>'>
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Actual Price" HeaderStyle-CssClass="actual" SortExpression="address">
            <ItemTemplate>
                <asp:Label ID="TradePriceField" runat="server" 
                        Text='<%#  DisplayMoney(Eval("UnitPrice").ToString())%>'>
                </asp:Label>
                <asp:Label ID="TradePriceFieldHidden" runat="server" 
                        Text='<%#  Eval("UnitPrice").ToString()%>'
                        Visible="false">
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="TotalPrice" HeaderText="Total" 
            HeaderStyle-CssClass="total" HeaderStyle-HorizontalAlign="Right" 
            DataFormatString="{0:C}" />
        <asp:TemplateField HeaderText="" HeaderStyle-CssClass="remove">
            <ItemTemplate>
                <asp:ImageButton ID="btnRemove" runat="server" 
                    ImageUrl="~/img/icons/cross.gif" 
                    Width="10" 
                    Height="10" 
                    AlternateText="Cancel"
                    CommandName="Remove" 
                    CommandArgument='<%# Eval("ProductId") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

其次,由于您未在任何地方定义FooterTemplate,因此收到错误。

答案 1 :(得分:0)

GridView FooterRow属性仅在调用RowDataBound方法后设置。您可以在GridView的DataBound方法中访问它。

Protected Sub gvShoppingCart_DataBound(ByVal sender As Object, ByVal e As EventArgs)
      Dim footerRow as GridViewRow = gvShoppingCart.FooterRow
End Sub
相关问题