Click here to Skip to main content
15,891,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have grid view and all column contains dynamic text box.

I want to calculate(Add) all the Grid view text box values and want to store in one label in side grid for every row..

How to calculate...?
Posted
Comments
JoCodes 5-Oct-13 5:58am    
whats the code you have tried so far???
ErBhati 5-Oct-13 6:27am    
<script type="text/javascript"" language="javascript">
function CalculateTotals() {
// get the GridView element in the page
var gv = document.getElementById("<%= Grid_Insert.ClientID %>");
// get the TextBox and Label control that's inside GridView template
var tb = gv.getElementsByTagName("input");
var lb = gv.getElementsByTagName("span");
// local variable declarations
var sub = 0;
var total = 0;
//tb.length gets the count of the TextBox controls within the GridView
var tbCount = tb.length;
//Loop based on TextBox count
for (var i = 0; i < tbCount; i++) {
{
//calculate the sub-total by added the values entered in the TextBox
sub = parseInt(tb[i ].value) ;
total += parseFloat(sub);
//check if the value of sub is NAN
if (isNaN(sub))
{
//if it is set a default value
lb[i].innerHTML = "0.00";
sub = 0;
}
else
{
//else print the actual value
lb[i].innerHTML =sub ;
}
}
}
</script>
ErBhati 5-Oct-13 6:28am    
but it gives error....

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

1 solution

Hi,
use this script by using JQuery

C#
function calculateGrandTotal() {
           var Amount = 0;
           $("#ctl00_MainContent_grid input[name*='txtAmount']").each(function (index) {
               //Check if number is not empty
               if ($.trim($(this).val()) != "")
               //Check if number is a valid integer
                   if (!isNaN($(this).val()))
                       Amount = Amount + parseFloat($(this).val());

           });

//this is used to display the sum of your value in label lbldisplay
           $("#ctl00_MainContent_grid span[id*='lblDisplay']").text(Amount);
       }



now call this function on javascript by using javascript event onkeyup='calculateGrandTotal();'
 
Share this answer
 
Comments
ErBhati 8-Oct-13 3:53am    
I have 12 txtbox inside gridview not a single....

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