计算价格单元格,离开我的动态表只能获得最后一行 [英] Counting price cell, off my dynamic table only gets last row

查看:56
本文介绍了计算价格单元格,离开我的动态表只能获得最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi, I'm making a dynamic table with textboxes, and from each row I will summarize the prices based on the choices they have made. Thif I have 10 rows, I have 10 prizes that I would like to summarize in a literal sum. But my counter does not work properly, it only takes the last row in my table and putting in total





我有什么试过:





What I have tried:

string total = "";
for (int j = 0; j < Rows; j++)
{
    TextBox t = (TextBox)(PlaceHolder1.FindControl("txtPrice_" + j));

    string[] values = {t.Text};
    double number;
    CultureInfo culture = null;

    foreach (string value in values)
    {
        try
        {
            culture = CultureInfo.CreateSpecificCulture("dk-DK");
            number = Double.Parse(value, culture);


            total = value.ToString();
        }
        catch (FormatException)
        {

            culture = CultureInfo.CreateSpecificCulture("dk-DK");


            total = value.ToString();
        }


    }
    litSum.Text = total;
}

推荐答案

好的。

你在做什么是这个:

Well yes.
What you are doing is this:
int total = 0;
for (int i = 1; i < 10; i++)
   {
   total = i;
   }
Console.WriteLine(total);

当结果总是9时你会感到惊讶。

当然它是:你每次循环时都会覆盖总计的值。

你需要做的是:

And you are surprised when the result is always "9".
Of course it is: you overwrite the value of total each time you go round the loop.
What you need to do is:

int total = 0;
for (int i = 1; i < 10; i++)
   {
   total += i;
   }
Console.WriteLine(total);

你会得到这笔钱。

但你不需要这么复杂!

And you will get the sum.
But you don't need to be so complicated!

culture = CultureInfo.CreateSpecificCulture("dk-DK");
NumberStyles style = NumberStyles.Number;
double number;
double total = 0.0;
foreach (string value in values)
    {
    if (Double.TryParse(value, style, culture, out number))
       {
       total += Number;
       }
    }
litSum.Text = total.ToString();


这篇关于计算价格单元格,离开我的动态表只能获得最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆