gpt4 book ai didi

c# - 在 Gridview 上汇总一列并显示在页脚中

转载 作者:行者123 更新时间:2023-11-30 23:25:10 25 4
gpt4 key购买 nike

我是 asp.net 的新手,我在尝试将我的购物车中的项目总值相加时遇到问题。我的代码不起作用,但我觉得我在正确的轨道上。有人可以帮助我吗?

购物车.aspx

<asp:TemplateField HeaderText="Item Total">
<ItemTemplate>
<asp:Label ID="lblItemTotal" runat="server" DataFormatString="{0:c2}"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text="Order Total: "></asp:Label>
</FooterTemplate>

ShoppingCart.aspx.cs

foreach (GridViewRow row in gvProductsList.Rows)
{
Label lblItemTotal = (Label)row.FindControl("lblItemTotal");//find itemTotal label
decimal decPrice = decimal.Parse(lblItemTotal.Text, NumberStyles.Currency);//convert to decimal
decimal newtotal =+ decPrice;
Label lblTotal = (row.FindControl("lblTotal")) as Label;
lblTotal.Text = "$" + Convert.ToString(newtotal);
}

我得到该行的 nullexceptionlblTotal.Text = "$"+ Convert.ToString(newtotal);

最佳答案

我自己是新手,但我认为在您的 foreach 循环中,您需要在指向页脚内的标签之前检查您正在查看的行是否确实是页脚,或者,我相信,您可以让你的页脚行:

var footerRow = gvProductsList.FooterRow;

编辑:我现在没有 VS,但试试这个:

decimal newtotal;
foreach (GridViewRow row in gvProductsList.Rows)
{
Label lblItemTotal = (Label)row.FindControl("lblItemTotal");
decimal decPrice = decimal.Parse(lblItemTotal.Text, NumberStyles.Currency);
newtotal =+ decPrice;
}
var footerRow = gvProductsList.FooterRow;
Label lblTotal = (footerRow.FindControl("lblTotal")) as Label;
lblTotal.Text = "$" + Convert.ToString(newtotal);

关于c# - 在 Gridview 上汇总一列并显示在页脚中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37444572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com