对于循环小计变为两倍 [英] For Loop subtotal becoming double

查看:100
本文介绍了对于循环小计变为两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


以下是我的C#代码,用于在gridview中查找动态文本框的总数和小计

根据我的代码,小计值正在变成双倍,例如500,我得到1000 ..

请纠正我

谢谢


Hi
Below is my C# code to find total and subtotal of dynamic textbox in gridview

as per my code the subtotal value is becoming double,example for 500 i am getting 1000 ..

please correct me

Thanks


for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
 TextBox box3  =   (TextBox)ItemGv.Rows[rowIndex].Cells[2].FindControl("qtytb");
 TextBox box4 =     (TextBox)ItemGv.Rows[rowIndex].Cells[3].FindControl("upricetb");
 TextBox box5 =     (TextBox)ItemGv.Rows[rowIndex].Cells[4].FindControl("amounttb");

                    drCurrentRow = dtCurrentTable.NewRow();

                    
                    drCurrentRow["Qty"] = box3.Text;
                    drCurrentRow["UnitPrice"] = box4.Text;                   
                    drCurrentRow["TotalAmount"] = box5.Text;
 //Total


                    for (int j = 0; j < ItemGv.Columns.Count; j++)
                    {
                        total = Convert.ToInt32(box3.Text) * Convert.ToSingle(box4.Text);
                        box5.Text = total.ToString();

                    }
//subtotal

                    for (int k = 0; k < ItemGv.Rows.Count; k++)
                    {
                       
                            subtotal = subtotal + Convert.ToSingle(box5.Text);
                            subtotaltb.Text = subtotal.ToString();
                        
                    
                    }


                    
                    rowIndex++;


[完整代码]
请问我已经发布了完整的代码


[complete code]
Could you pls have a look i have posted complete code

private void AddNewRowToGrid()
    {
        int rowIndex = 0;
        float total;
        float subtotal = 0;

      
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

            DataRow drCurrentRow = null;

            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values

                    TextBox box1 = (TextBox)ItemGv.Rows[rowIndex].Cells[0].FindControl("itemNotb");

                    TextBox box2 = (TextBox)ItemGv.Rows[rowIndex].Cells[1].FindControl("desctb");
                    TextBox box3 = (TextBox)ItemGv.Rows[rowIndex].Cells[2].FindControl("qtytb");
                    TextBox box4 = (TextBox)ItemGv.Rows[rowIndex].Cells[3].FindControl("upricetb");
                    TextBox box5 = (TextBox)ItemGv.Rows[rowIndex].Cells[4].FindControl("amounttb");

                    drCurrentRow = dtCurrentTable.NewRow();

                    drCurrentRow["ItemNo"] = box1.Text;
                    drCurrentRow["Description"] = box2.Text;
                    drCurrentRow["Qty"] = box3.Text;
                    drCurrentRow["UnitPrice"] = box4.Text;                   
                    drCurrentRow["TotalAmount"] = box5.Text;

                    for (int j = 0; j < ItemGv.Columns.Count; j++)
                    {
                        total = Convert.ToInt32(box3.Text) * Convert.ToSingle(box4.Text);
                        box5.Text = total.ToString();

                    }


                    for (int k = 0; k < ItemGv.Rows.Count; k++)
                    {
                       
                            subtotal = subtotal + Convert.ToSingle(box5.Text);
                            subtotaltb.Text = subtotal.ToString();
                        
                    
                    }


                    
                    rowIndex++;

                }

推荐答案

我不禁想到您需要退后一步,重新考虑您的工作. > 我不确定您希望该代码执行什么操作,但我认为这与您期望的不完全相同.

我说这的原因是,您有两个内部循环,都在一个外部对象ItemGv上工作,在第一个循环中,每次循环时您执行相同的代码,而在第二个循环中,您仅将乘积的结果有效地相乘第一次循环,但ItemGv的行计数.这似乎很奇怪-而不是给定变量名的期望.

考虑一下您的代码应该做什么,并尝试弄清楚为什么首先放置循环!如果这样不能解决问题,请在例程的开始处放置一个断点,然后逐步执行-我认为它并没有达到您的期望.
I can''t help thinking that you need to go back a step, and re-think what you are doing.
I''m not sure what you expect that code to do, but I don''t think it is quite what you expect.

The reason I say this is that you have two inner loops, both working on a external object ItemGv, buit inthe first loop you do the same code each time you go round, and in the second you just effectively multiply the result of the first loop but the rows count of the ItemGv. This seems odd - and not what I would expect given the variable names.

Have a think about what your code should be doing, and try to work out why you put the loops in in the first place! If that doesn''t sort it out, put a breakpoint at the start of the routine, and single step through it - I don''t think it does what you expect.


for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
 TextBox box3  =   (TextBox)ItemGv.Rows[rowIndex].Cells[2].FindControl("qtytb");
 TextBox box4 =     (TextBox)ItemGv.Rows[rowIndex].Cells[3].FindControl("upricetb");
 TextBox box5 =     (TextBox)ItemGv.Rows[rowIndex].Cells[4].FindControl("amounttb");
 
                    drCurrentRow = dtCurrentTable.NewRow();
                    
                    drCurrentRow["Qty"] = box3.Text;
                    drCurrentRow["UnitPrice"] = box4.Text;                   
                    drCurrentRow["TotalAmount"] = box5.Text;
                    
                        total = Convert.ToInt32(box3.Text) * Convert.ToSingle(box4.Text);
                        box5.Text = total.ToString();                

                       
                            subtotal = subtotal + Convert.ToSingle(box5.Text);
                            subtotaltb.Text = subtotal.ToString();
                        
                    
                   

                    
                    rowIndex++;


这篇关于对于循环小计变为两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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