Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Javascript Summation of values in a gridview results in NaN :P

Avatar of tdartt
tdartt asked on
JavaScriptASP.NETEditors IDEs
20 Comments1 Solution1037 ViewsLast Modified:
I am attempting to sum all the values of a column in a gridview client side so I can show the order subtotal on the screen without causing a postback.

When I iterate through the gridview row, the objStr will print as the value of the line (ex: 26.20 ) if I output it to the screen, but when I attempt to use parseFloat(objstr) or Number(objStr).tofixed(2) it always results in Nan :(

Any one have any spiffy ideas?
Thanks :)
function calcPrice(txtQtyId,decPrice,lblPriceID) {
            document.getElementById(lblPriceID).innerHTML = "$"+formatAsMoney(document.getElementById(txtQtyId).value * decPrice);
        function formatAsMoney(ObjVal)
         //Obj.value = formatAsMny(Obj.value)
         mnt = ObjVal;
         mnt -= 0;
         mnt = (Math.round(mnt*100))/100;
         ObjVal = (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
         if (isNaN(ObjVal)) {ObjVal ="0.00";}
         return ObjVal;
        function SumTotal()
            var grid = document.getElementById('<%= gvView.ClientID %>');           
            var total = 0;
            var curPrice = 0;
            var objstr = ' ';
            var objNum = 0;
            if (grid.rows.length > 0)
                //loop starts from 1. rows[0] points to the header.
                for (i=1; i<grid.rows.length; i++)
                // for (i=1; i<2; i++)
                    //get the reference of first column
                    objStr = trim(grid.rows[i].cells[6].innerHTML.replace("$",""));
                    var objNum = Number(objstr).toFixed(2)
                    document.write("|-"+objNum+ "-|");
                    document.write("|"+objStr+ "|");
                    objStr = parseFloat(objStr).toFixed(2)
                    document.write(objStr + "<br />");
                    objStr = Number(objStr);
                    //if (isNaN(calcStr)==false) 
                    // total += (isNaN(objStr))?0:parseFloat(objStr);
                          total += parseFloat(objStr);
                //        document.writeln(objStr);
                  //      document.writeln(total);
               // document.writeln('muffins!');
                // document.writeln(total);
                document.getElementById('<%= lblSubTotal.ClientID %>').innerHTML = total;
        function trim(str, chars) {
            return ltrim(rtrim(str, chars), chars);
        function ltrim(str, chars) {
            chars = chars || "\\s";
            return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
        function rtrim(str, chars) {
            chars = chars || "\\s";
            return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
Avatar of nognew
nognewFlag of United Kingdom of Great Britain and Northern Ireland image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 20 Comments.
See Answers