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

C#
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();
}
Posted
Updated 16-Jul-14 0:36am
v2
Comments
ashok rathod 16-Jul-14 6:29am    
your questino is quite unclear. Please format it properly and mention exact requirement.
ArunRajendra 16-Jul-14 6:40am    
Post the final output you are getting in lblGrandTot.Text and lblQty.Text

I think this is your problem:

C#
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
C#
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.
 
Share this answer
 
v2
Not sure I am just guessing on this two lines.

C#
tot = Convert.ToDouble(tot + lblTotal.Text);
qty = Convert.ToInt32(qty + txt_Qty.Text);


change it to
C#
tot = tot + Convert.ToDouble(lblTotal.Text);
qty = qty + Convert.ToInt32(txt_Qty.Text);
 
Share this answer
 
v2
Hello!
Try below code:
C#
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!
 
Share this answer
 
v2
Change from
Quote:
tot = Convert.ToDouble(tot + lblTotal.Text);
to
C#
tot = tot + Convert.ToDouble(lblTotal.Text);
and so on.
 
Share this answer
 
i want to sumup the GrandtotalAmount(lblGrandTot ) and GrandTotalQty(lblQty) in footerrow(GridView),
when we entering the value in (txt_Qty) ..

Please Help me...

The below code concatenates the (lblTotal.Text),values, suppose 1st val=4 and 2nd val=5 means it shows 45... But i want to add(suppose 1st val=4 and 2nd val=5 means ans should be 9)
The below code concatenates the (txt_Qty .Text),values, suppose 1st val=4 and 2nd val=5 means it shows 45... But i want to add(suppose 1st val=4 and 2nd val=5 means ans should be 9)

This is my code...
int len = GV_Product.Rows.Count;
double tot = 0.0;
int qty = 0;

for (int i = 0; i < len; i++)
{
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();//This code concatenate the (lblTotal.Text) values, suppose 1st val=4 and 2nd val=5 means it shows 45... But i want to add(suppose 1st val=4 and 2nd val=5 means ans should be 9)
lblQty.Text = qty.ToString();//This code concatenate the (lblTotal.Text) values, suppose 1st val=4 and 2nd val=5 means it shows 45... But i want to add(suppose 1st val=4 and 2nd val=5 means ans should be 9)




}
 
Share this answer
 
Comments
Marcin Kozub 16-Jul-14 7:10am    
Update your question instead posting an answer. And format your code!

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