Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 how to calculate total in grid view using 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 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
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);

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

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
Simply JavaScript - Free 150 Page Preview!
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Simply SQL - Free 111 Page Preview!