AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 how to calculate total in grid view using asp.net with c#
Posted by charan on  3/31/2010 5:14:03 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
hi iam using asp.net with c# with sql server


in my grid there is checkbox in the 1st column, then product,price,quantity,total columns available
when user enters the value in quantity field i should get the value in total field (ie) price* quantity
and finally i should get the grandtotal value in the footerl

can you give me a example which helps me

Post Your Reply Post New Question

 Viewer's Reply
Calculate GridView column Total Dynamically
Answered By Moderator1 on 4/1/2010 10:47:40 PM
Hi,
The logic to calculate grand total through gridview column is below,

Step 1: Declare a page scope variable
double dbltotal=0;
Step 2: In GridView1_RowDataBound event, find the price and quantity field. then
if (e.Row.RowType == DataControlRowType.DataRow)
{
double price = (write code to find price column);
double quantity=(write code to find quantity column);
dbltotal+=(Convert.ToDouble(price)*Convert.ToDouble(quantity));
}

if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[Your Column Number where you want to show Grand Total].Text = dbltotal;
}

The above is just logic..
 
i want when user enters quantity value the total should be display dynamically in total column
Answered By charan on 4/3/2010 5:14:44 AM
i want when user enters quantity value the total should be display dynamically in total column

but the above code u given for getting total in a footer.please can u give example
 
Write JavaScript Code in TextBox OnChange Event
Answered By Moderator1 on 4/4/2010 10:00:05 AM
Hi,

Then you need to write the javascript code in both the price and quantity textbox onchange events to calculate the total values.

txtprice.Attributes.Add("onchange", "fnCalculateTotal()");
txtquantity.Attributes.Add("onchange", "fnCalculateTotal()");

Then in JavaScript,

function fnCalculateTotal(){
var price=document.getElementsById("txtprice").value;
var quantity=document.getElementsById("txtquantity").value;
document.getElementsById("txttotal").value = price * quantity;
}

The above is just logic.. Complete the code by correcting the logic and syntax.
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
No Nonsense XML Web Development With PHP - Free 146 Page Preview!
Take Advantage of Oracle's 2 Day DBA Course
The Art & Science of JavaScript - Free 100 Page Preview!