Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Gridview中向上汇总列并显示在页脚中_C#_Asp.net_Gridview - Fatal编程技术网

C# 在Gridview中向上汇总列并显示在页脚中

C# 在Gridview中向上汇总列并显示在页脚中,c#,asp.net,gridview,C#,Asp.net,Gridview,我是asp.net新手,在我的shoppingcart中添加itemstotal值时遇到问题。我的代码不起作用,但我觉得我走在了正确的轨道上。有人能帮我吗 ShoppingCart.aspx <asp:TemplateField HeaderText="Item Total"> <ItemTemplate> <asp:Label ID="lblItemTotal" runat="server" DataFormatString="{0:c2

我是asp.net新手,在我的shoppingcart中添加itemstotal值时遇到问题。我的代码不起作用,但我觉得我走在了正确的轨道上。有人能帮我吗

ShoppingCart.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>
我得到一个空的例外
lblTotal.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);

您的
foreach
循环在哪个事件中?它在按钮单击事件中更新购物车。我是否在指向标签之前添加该行?我得到了一个NullReferenceException。我试过了,得到了一些值显示在总数中,但是显示的数字只是列表上最后一个产品的产品总数,而不是总和。@Jamie Oh!切换等号和加号。让我知道这是否有效。是的,确实有效!非常感谢你!!
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);