【问题标题】:ASP.NET Dynamically Adding a GridviewASP.NET 动态添加 Gridview
【发布时间】:2011-08-17 23:42:22
【问题描述】:

我有一个这样的网格视图

<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

知道怎么回事吗?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案2】:

      首先,您的 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,因此您会收到错误消息。

      【讨论】:

      • 你能告诉我如何添加一个页脚行,该行将具有一些功能,如 Protected Sub gvShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.RowDataBound ' 如果我们正在绑定页脚行,让我们添加我们的总数 If e.Row.RowType = DataControlRowType.Footer Then e.Row.Cells(5).Text = "Total Cost:" e.Row.Cells(6) .Text = ShoppingCart.Instance.GetAmountSaved().ToString("C") End If End Sub
      • 我添加了空页脚模板,即 但仍然得到错误
      猜你喜欢
      • 2010-09-19
      • 2010-10-27
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多