计算网格视图购物车的总价格 [英] Calculate Total Price for grid view cart
本文介绍了计算网格视图购物车的总价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个网格视图表。
Hi, I have this grid view table.
<asp:GridView ID="gv_CartProduct" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="title" HeaderText="Product:"/>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="tb_quantity" runat="server" DataField="qty" Text='<%# Eval("qty") %>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="price" HeaderText="Price" />
</Columns>
</asp:GridView>
我想通过乘以数量和价格来计算价格。但它一直给我带来问题。我不断收到此错误消息:
I wanted to calculate the price by multiplying quantity and price. But it has been giving me problem. I keep getting this error message:
Message "Input string was not in a correct format." string
这是我使用的代码计算总价:
This is the code I use the calculate the total price:
protected void CalculateCartSums()
{
int amount = 0;
int qty = 0;
int rowTotal = 0;
int subTotal = 0;
int total = 0;
foreach (GridViewRow row in gv_CartProduct.Rows)
{
qty = Convert.ToInt32(row.Cells[1].Text);
amount = Convert.ToInt32(row.Cells[2].Text);
rowTotal = qty * amount;
subTotal += rowTotal;
}
total = subTotal;
lblPriceTotal.Text = Convert.ToString(total);
}
非常感谢。谢谢
Much appreciated. Thank You
推荐答案
数量
是一个TextBox
,它在TemplateField
里面。
你必须得到它...
Quantity
is aTextBox
, which is inside theTemplateField
.
You have to get it like...
TextBox txtQuantity = (TextBox)row.FindControl("tb_quantity");
qty = Convert.ToInt32(txtQuantity.Text);
这篇关于计算网格视图购物车的总价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文