Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can anyone help?



how to retrive price and quantity from gridview and show it in footer also & how to calculate totalprice in asp.net



C#
protected void totalprice()
    {
        int quantity = int.Parse("txtquantity");
     float price =float.Parse("price");
     float totalprice = float.Parse("totalPrice");
     totalprice=quantity * price;
}


is thi correct?
Posted
Updated 2-Dec-11 18:29pm
v2
Comments
Karthik Harve 3-Dec-11 0:24am    
what are those price and quantity..? are they row values, column values..?? do you want calculate total price at the footer of gridview..?? be specific and add some code, if you have tried something.
sathiyak 3-Dec-11 1:53am    
row values........yes i want to calculate totalprice but not in footer outer grid....
Prince Antony G 3-Dec-11 2:13am    
why u repost the question again and again?
sathiyak 3-Dec-11 2:30am    
helo...i m not reposting.....

C#
function calculateGridTotal() {  // call this on body load

        var tmpgvRenewal = document.getElementById("gvRenewal");  // this is the gridis
        var Price = 0.0;
var Quantity = 0.0;
var TotalPrice = 0.0;
        var arrstr = tmpgvRenewal.id + "_ctl";

        for (var i = 2; i <= tmpgvRenewal.childNodes[0].childNodes.length - 1; i++) {   //search for all row of grid view and calculate whatever you want
            if (parseInt(i) < 10) {
                var c = "0" + i;
            }
            else {
                var c = i;
            }
            var lblQuantity = document.getElementById(arrstr + c + "_lblQuantity").innerHTML;
            var lblPrice= document.getElementById(arrstr + c + "_lblPrice").innerHTML;

            lblQuantity= lblQuantity.replace(/,/g, '');  // if that value is like(100,000,00)
            lblPrice = lblPrice.replace(/,/g, '');


            if (lblQuantity != "")
                Quantity = Quantity + parseFloat(lblQuantity);

            if (lblPrice!= "")
              Price = Price + parseFloat(lblPrice);

        }

TotalPrice = Price * Quantity;
        tmpgvRenewal.rows[tmpgvRenewal.rows.length - 1].cells[2].innerHTML = Quantity;   // assign it to footer row of grid(make footer row visible = true) (Total Quantity)
        tmpgvRenewal.rows[tmpgvRenewal.rows.length - 1].cells[3].innerHTML = Price;
        tmpgvRenewal.rows[tmpgvRenewal.rows.length - 1].cells[4].innerHTML = TotalPrice;

    }



try this java script. to calculate total of grid element and assin into footer.
 
Share this answer
 
make AutoGenerateSelectButton=true,
on rowcommand of gridview
C#
protected void fect_taskid(object sender, GridViewCommandEventArgs e)
   {
               int index = Convert.ToInt32(e.CommandArgument);
               GridViewRow row = GridView1.Rows[index];
               string pr = row.Cells[1].Text;//1 will be the index of Price
               string qt=row.Cells[2].Text;
               float lbltotalprice=float.Parse(int.Parse(qt)*float.Parse(pr))
   }
 
Share this answer
 
you can do like this .

C#
Protected void Totalprice()
{
    foreach(GridViewRow gvRow in GridViewName.Rows)
    {
        int Quantity = Convert.ToInt32(((TextBox)gvRow.FindControl("txtquantity")).Text)
        int Price = float.tryParse(((Label)gvRow.FindControl("lblPrice")).Text)
    float TotalPrice = Quantity * Price;

    // find here footer Control of gridview and set the value.
    // first you have to make sure ShowFooterRow is true

    lblFooterTotalPrice = (Label)gvrow.FindControl("lblfootertotalPrice");
    lblFooterTotalPrice.Text = TotalPrice;
    }

}
 
Share this answer
 
Comments
sathiyak 3-Dec-11 2:09am    
i dont want to calculate at the footer...
S.P.Tiwari 3-Dec-11 2:34am    
can you see once your question.? waht you have written.you want to calculate and show into footer...make sure what you actualy want.+

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