62,074
社区成员
发帖
与我相关
我的任务
分享
<asp:TemplateField HeaderText="UnitPrice" SortExpression="UnitPrice">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Calc1((decimal)Eval("UnitPrice")) %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="LabelSum" runat="server" Text='<%# sum %>' />
</FooterTemplate>
</asp:TemplateField>
private decimal sum = 0m;
protected decimal Calc1(decimal x)
{
sum += x;
return x;
}
<asp:TemplateField HeaderText="UnitPrice" SortExpression="UnitPrice">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="LabelSum" runat="server" />
</FooterTemplate>
</asp:TemplateField>
private decimal sum = 0m;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView drv = e.Row.DataItem as System.Data.DataRowView;
sum += Convert.ToDecimal(drv["UnitPrice"]);
}
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
Label labelSum = GridView1.FooterRow.FindControl("LabelSum") as Label;
labelSum.Text = sum.ToString();
}
网上代码太多了!
简单说下实现过程
在gridview的rowdatabound事件中进行列值的累加
然后在脚注中显示!!
<footerTemplate>
//...code
</footerTemplate>