总结c#.net中的值 [英] Sum up the Values in c#.net
问题描述
我想在输入值时总结GrandtotalAmount和GrandTotalQty ......
以下代码正在执行连接操作...
我想总结....
请帮助我.. 。
int len = GV_Product.Rows.Count;
double tot = 0 。 0 跨度>;
int qty = 0 ;
for ( int i = 0 ; i < len; i ++)
{
CheckBox chk =(CheckBox)GV_Product.Rows [i]。 FindControl( chk);
标签lblPrdName =(标签)GV_Product.Rows [i] .FindControl( lblPrdName );
标签lblPrdPrice =(标签)GV_Product.Rows [i] .FindControl( lblPrdPrice );
TextBox txt_Qty =(TextBox)GV_Product.Rows [i] .FindControl( txt_Qty );
Label lblQty =(Label)GV_Product.FooterRow.FindControl( lblQty);
标签lblTotal =(标签)GV_Product.Rows [i] .FindControl( lblTotal );
标签lblGrandTot =(标签)GV_Product.FooterRow.FindControl( lblGrandTot);
lblTotal.Text = Convert.ToInt32(
Convert.ToInt32(txt_Qty.Text)
*转换.ToInt32(lblPrdPrice.Text)
).ToString();
tot = Convert.ToDouble(tot + lblTotal.Text);
qty = Convert.ToInt32(qty + txt_Qty.Text);
lblGrandTot.Text = tot.ToString();
lblQty.Text = qty.ToString();
}
我认为这是你的问题:
lblTotal.Text = Convert.ToInt32(
Convert.ToInt32(txt_Qty.Text)
* Convert.ToInt32(lblPrdPrice.Text)
).ToString ();
tot = Convert.ToDouble(tot + lblTotal.Text);
qty = Convert.ToInt32(qty + txt_Qty.Text);
更改为
lblTotal.Text =(Convert.ToInt32(txt_Qty.Text)
* Convert.ToInt32(lblPrdPrice.Text))。ToString();
tot + = Convert.ToDouble(lblTotal.Text);
qty + = Convert.ToInt32(txt_Qty.Text);
你有一个非常奇怪的设计,声明你的GUI组件在这样的循环中,但是我要判断谁。
不确定我只是猜测这两行。
tot = Convert.ToDouble(tot + lblTotal.Text);
qty = Convert.ToInt32(qty + txt_Qty.Text);
将其更改为
tot = tot + Convert.ToDouble(lblTotal.Text);
qty = qty + Convert.ToInt32(txt_Qty.Text);
您好!
尝试以下代码:
int len = GV_Product.Rows.Count;
double totalValue = 0 。 0 跨度>;
int totalQuantity = 0 ;
bool error = false ;
for ( int i = 0 ; i < len; i ++)
{
CheckBox chk =(CheckBox)GV_Product.Rows [i] .FindControl( chk);
标签lblPrdName =(标签)GV_Product.Rows [i] .FindControl( lblPrdName );
标签lblPrdPrice =(标签)GV_Product.Rows [i] .FindControl( lblPrdPrice );
TextBox txt_Qty =(TextBox)GV_Product.Rows [i] .FindControl( txt_Qty );
标签lblTotal =(标签)GV_Product.Rows [i] .FindControl( lblTotal );
int price = 0 ;
int quantity = 0 ;
if (!Int32.TryParse(lblPrdPrice.Text, out 价格) )
{
error = true ;
}
if (!Int32.TryParse(txt_Qty.Text, out 数量))
{
error = true ;
}
if (!error)
{
int totalRow =价格*数量;
totalQuantity + =数量;
totalValue + = totalRow;
lblTotal.Text = totalRow.ToString();
}
其他
{
// < span class =code-comment>显示行错误或执行其他操作
lblTotal.Text = 错误!跨度>;
}
}
// 您可以移动代码进行更新循环外部的页脚字段仅执行一次不在每个循环过程中
标签lblGrandTot =(标签)GV_Product.FooterRow.FindControl( lblGrandTot);
Label lblQty =(Label)GV_Product.FooterRow.FindControl( lblQty);
lblGrandTot.Text = totalValue.ToString();
lblQty.Text = totalQuantity.ToString();
我的建议:在你将字符串转换为其他类型之前你应该definitelly应该检查它的价值。使用简单类型的TryParse方法来检查string是否是类型的正确值。
http://msdn.microsoft.com/pl-pl/library/f02979c7%28v=vs.110%29.aspx [<一个href =http://msdn.microsoft.com/pl-pl/library/f02979c7%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]
和解析双/小数值请参考以下链接:
http://stackoverflow.com/questions/1354924/how-do-i-parse-a -string-with-a-decimal-point-to-a-double [ ^ ]
干杯!
i want to sumup the GrandtotalAmount and GrandTotalQty...
when we entering the values..
The below code is doing concatenate operation...
I want to sumup....
Please Help me...
int len = GV_Product.Rows.Count;
double tot = 0.0;
int qty = 0;
for (int i = 0; i < len; i++)
{
CheckBox chk = (CheckBox)GV_Product.Rows[i].FindControl("chk");
Label lblPrdName = (Label)GV_Product.Rows[i].FindControl("lblPrdName");
Label lblPrdPrice = (Label)GV_Product.Rows[i].FindControl("lblPrdPrice");
TextBox txt_Qty = (TextBox)GV_Product.Rows[i].FindControl("txt_Qty");
Label lblQty = (Label)GV_Product.FooterRow.FindControl("lblQty");
Label lblTotal = (Label)GV_Product.Rows[i].FindControl("lblTotal");
Label lblGrandTot = (Label)GV_Product.FooterRow.FindControl("lblGrandTot");
lblTotal.Text = Convert.ToInt32(
Convert.ToInt32(txt_Qty.Text)
* Convert.ToInt32(lblPrdPrice.Text)
).ToString();
tot = Convert.ToDouble(tot + lblTotal.Text);
qty = Convert.ToInt32(qty + txt_Qty.Text);
lblGrandTot.Text = tot.ToString();
lblQty.Text = qty.ToString();
}
I think this is your problem:
lblTotal.Text = Convert.ToInt32( Convert.ToInt32(txt_Qty.Text) * Convert.ToInt32(lblPrdPrice.Text) ).ToString(); tot = Convert.ToDouble(tot + lblTotal.Text); qty = Convert.ToInt32(qty + txt_Qty.Text);
change to
lblTotal.Text = (Convert.ToInt32(txt_Qty.Text) * Convert.ToInt32(lblPrdPrice.Text)).ToString(); tot += Convert.ToDouble(lblTotal.Text); qty += Convert.ToInt32(txt_Qty.Text);
You have a very strange design with declaring your GUI components inside a loop like that, but who am I to judge.
Not sure I am just guessing on this two lines.
tot = Convert.ToDouble(tot + lblTotal.Text); qty = Convert.ToInt32(qty + txt_Qty.Text);
change it to
tot = tot + Convert.ToDouble(lblTotal.Text); qty = qty + Convert.ToInt32(txt_Qty.Text);
Hello!
Try below code:
int len = GV_Product.Rows.Count; double totalValue = 0.0; int totalQuantity = 0; bool error = false; for (int i = 0; i < len; i++) { CheckBox chk = (CheckBox)GV_Product.Rows[i].FindControl("chk"); Label lblPrdName = (Label)GV_Product.Rows[i].FindControl("lblPrdName"); Label lblPrdPrice = (Label)GV_Product.Rows[i].FindControl("lblPrdPrice"); TextBox txt_Qty = (TextBox)GV_Product.Rows[i].FindControl("txt_Qty"); Label lblTotal = (Label)GV_Product.Rows[i].FindControl("lblTotal"); int price = 0; int quantity = 0; if (!Int32.TryParse(lblPrdPrice.Text, out price)) { error = true; } if (!Int32.TryParse(txt_Qty.Text, out quantity)) { error = true; } if (!error) { int totalRow = price * quantity; totalQuantity += quantity; totalValue += totalRow; lblTotal.Text = totalRow.ToString(); } else { // Display row error or do something else lblTotal.Text = "Error!"; } } // You can move code for updating footer fields outside the loop to do it just one time not in every loop course Label lblGrandTot = (Label)GV_Product.FooterRow.FindControl("lblGrandTot"); Label lblQty = (Label)GV_Product.FooterRow.FindControl("lblQty"); lblGrandTot.Text = totalValue.ToString(); lblQty.Text = totalQuantity.ToString();
My suggestion: Before you convert string to other types you definitelly should checking its value. Use TryParse methods of simple types to check if string is proper value for type.
http://msdn.microsoft.com/pl-pl/library/f02979c7%28v=vs.110%29.aspx[^]
and for parsing double/decimal values refer to this link:
http://stackoverflow.com/questions/1354924/how-do-i-parse-a-string-with-a-decimal-point-to-a-double[^]
Cheers!
这篇关于总结c#.net中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!