Click here to Skip to main content
15,915,734 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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++;

                }
Posted
Updated 10-Dec-11 22:15pm
v2
Comments
Sergey Alexandrovich Kryukov 11-Dec-11 2:25am    
Relevant part of code is not show. It's apparent that the problem is outside. Where is the declaration of "subtotal"? Where is the method of the shown code? Also, just run it all under debugger and you will see.
--SA
Lancy.net 11-Dec-11 2:33am    
Thanks...
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++;

}

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.
 
Share this answer
 
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++;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900