在gridview的页脚中创建一个新行

时间:2014-07-07 19:15:42

标签: c# asp.net gridview

我有一个网格视图,其中包含一些列,然后是FooterStyle

我想在页脚下添加一行。

这是我的GridView:

<asp:GridView ID="cartGrid" runat="server" AutoGenerateColumns="false" 
    ShowFooter="true" CssClass="cartTbl" onrowdatabound="cartGrid_RowDataBound" OnRowEditing="cartGrid_RowEditing" OnRowCancelingEdit="cartGrid_RowCancelingEdit"
    onrowupdating="cartGrid_RowUpdating" EmptyDataText="There are no items in your cart.">
    <Columns>
        <asp:BoundField DataField="ItemNo" HeaderText="Item No." ReadOnly="true" />
        <asp:BoundField DataField="ItemDesc" HeaderText="Item Description" ReadOnly="true" />
        <asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:C}" ReadOnly="true" />
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:Label ID="qLbl" runat="server" Text='<%# Bind("numItems") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="qtb" runat="server" Text='<%# Bind("numItems") %>' CssClass="qtb" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="TotalItemPrice" HeaderText="Item Total" DataFormatString="{0:C}" ReadOnly="true" />
        <asp:BoundField DataField="orderID" ReadOnly="true" ControlStyle-CssClass="hidden" />
        <asp:BoundField DataField="itemID" ReadOnly="true" ControlStyle-CssClass="hidden" />
        <asp:CommandField ShowEditButton="true" />
    </Columns>
    <FooterStyle CssClass="cartFooter" />
</asp:GridView>

我如何在下面添加一行?我假设它将在代码背后,但我不知道该怎么做,因为这是我第一次使用asp.net。任何帮助将非常感激。谢谢!

另外,如果我有任何帮助,请告诉我。就像我上面说的那样,我对这一切都很陌生。

2 个答案:

答案 0 :(得分:3)

如果要向页脚添加其他行,则必须抓住OnRowDataBound事件

protected void cartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                TableRow tableRow = new TableRow();
                TableCell cell1 = new TableCell();
                cell1.Text = "Add your your content here"; 
                cell1.ColumnSpan = 8; // You can change this. If you want different cells you can add as many cells as you need
                tableRow.Controls.Add(cell1);
                e.Row.NamingContainer.Controls.Add(tableRow);
                // You can add additional rows like this.
            }
        }

答案 1 :(得分:0)

您需要执行以下操作:

protected void GridView1_RowData(object sender,GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.Footer)
   {
      TableRow tr = new TableRow();

      TableCell cell1 = new TableCell();
      cell1.Text = "A Button";

      TableCell cell2 = new TableCell();
      Button button = new Button();

      button.ID = "button1";
      button.Text = "Click me!";
      button.Click += new EventHandler(button_Click);
      cell2.Controls.Add(button);

      e.Row.Cells.Clear();
      e.Row.Cells.Add(cell1);
      e.Row.Cells.Add(cell2);

    }    
}

protected void button_Click(object sender, EventArgs e)
{
   String text = e.ToString();
}