Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can Anyone Give Code for calculate total price depands on gridview price,quantity in each row...ple help me....

my coding calculate only last row of grid...

CSS
pro_id    pro_name    quantity     price Edit Update Delete

 55       flowers         2         300
 60     party flower      1         680



              Shopping Cart Details

                                 TotalPrice : 680  //  Error


DeliveryPlace :   chennai
DeliveryCharge :   250
Tax (in %) :        10
Shipping :
NetPrice :






Error Coding:


C#
public void totalprice()
   {
       float pricee;
       int quantityy;
       foreach (GridViewRow row in GridView1.Rows)
       {
           int pro_id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex][0]);
           pricee = dal.getprice(pro_id);
           quantityy = dal.getquantity(pro_id);
           float totalprice = quantityy * pricee;
           lbltprice.Text = totalprice.ToString();
       }
   }
Posted

try this code:

C#
public void totalprice()
   {
       float pricee;
       int quantityy;
      float totalprice;
       foreach (GridViewRow row in GridView1.Rows)
       {
           int pro_id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex][0]);
           pricee = dal.getprice(pro_id);
           quantityy = dal.getquantity(pro_id);
           totalprice += quantityy * pricee;
       }
      lbltprice.Text = totalprice.ToString();
   }


hope it helps!

don't forget to vote :)
 
Share this answer
 
Comments
M.Narmatha 7-Dec-11 5:33am    
it shows error like : use of unassigned variable totalprice
Pramod Harithsa 7-Dec-11 8:02am    
just initialise it to zero. . that should work
M.Narmatha 7-Dec-11 22:59pm    
thank u so much... its working..
C#
// this is a javascript code used to retrieve gridview row value
function Read_Data()
{
    var str=0;
    // here grid_list is the id of your asp.net gridview
    // grid_list.ClientID gives asp.net server executed id of the gridview
    // inside the for loop cells[3] which denote 3 column from the gridview
    // str is a variable which will sum up all your row values on the column
    var Grid_Table = document.getElementById('<%=grid_list.ClientID%>');
    for(var row=1; row<Grid_Table.rows.length; row++)
    {
                    if(document.all)
                        str=str+Number(Grid_Table.rows[row].cells[3].innerText);

                    else
                        str=str+Number(Grid_Table.rows[row].cells[3].textContent);

    }
    alert(str);
    return false;
}
 
Share this answer
 
v2
Comments
vino2012 7-Dec-11 6:56am    
Reply me if the code works for your issue...........

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