在gridview的页脚创建一个新行
本文关键字:一个 新行 创建 gridview | 更新日期: 2023-09-27 18:11:55
我有一个网格视图里面有一些列然后是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。任何帮助都将非常感激。谢谢!
还有,如果我遗漏了什么有用的东西,请让我知道。就像我上面说的,我对这一切都很陌生。如果您想在页脚添加额外的行,您必须抓住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.
}
}
您需要这样做:
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();
}