Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do we do the calculations (like in MS Excel)in Datagrid in c#.net.

Means automatic updates when we enter the Gridcell then the result will be updated automatically.
Posted
Updated 28-Dec-11 23:28pm
v3

Hi,

You need to store total line count in hidden fields and then loop through count and all controls have a patern with increasing number. Identify ur controls to implement ur business logic using java script on focus lost of ur control.


I have emplemented same. I think It will help you.
C#
function CalculateRes() {
            var totalColReserve = 0;
            var totalColPaid = 0;
            var totalColrec = 0;
            var totalColTotal = 0;

// Hidden field to get count
            var Count = document.getElementById("hidCount").value;

            for (var i = 2; i <= Count; i++) {
                var inc = i;
                if (i < 10) {
                    inc = "0" + i;
                }
                if (i != Count) {
                    var ReserveVal = document.getElementById("GridReserveDetail_ctl" + inc + "_txtReserveAmt");
                    var paidVal = document.getElementById("GridReserveDetail_ctl" + inc + "_txtPaidAmt");
                    var RecVal = document.getElementById("GridReserveDetail_ctl" + inc + "_txtRecoveryAmt");
          var RtotalVal = document.getElementById("GridReserveDetail_ctl" + inc + "_txtTotalAmt");

                    ReserveVal.value = addCommas(parseFloat(unComma(ReserveVal.value)).toFixed(2));
                    paidVal.value = addCommas(parseFloat(unComma(paidVal.value)).toFixed(2));
                    RecVal.value = addCommas(parseFloat(unComma(RecVal.value)).toFixed(2));
                    RtotalVal.value = addCommas(parseFloat(unComma(RtotalVal.value)).toFixed(2));

                    if (ReserveVal.value == "0") {
                        ReserveVal.value = "0.00";
                    }
                    if (paidVal.value == "0") {
                        paidVal.value = "0.00";
                    }
                    if (RecVal.value == "0") {
                        RecVal.value = "0.00";
                    }

                }
                if (i == Count) {
                    inc = i + 1;
                    if (inc < 10) {
                        inc = "0" + inc;
                    }
                                                  }
                RtotalVal.value = parseFloat(unComma(ReserveVal.value)) + parseFloat(unComma(paidVal.value)) - parseFloat(unComma(RecVal.value));
                RtotalVal.value = addCommas(parseFloat(unComma(RtotalVal.value)).toFixed(2));
                if (RtotalVal.value == "0") {
                    RtotalVal.value = "0.00";
                }
                totalColReserve = parseFloat(unComma(totalColReserve)) + parseFloat(unComma(ReserveVal.value));
                totalColPaid = parseFloat(unComma(totalColPaid)) + parseFloat(unComma(paidVal.value));
                totalColrec = parseFloat(unComma(totalColrec)) + parseFloat(unComma(RecVal.value));
                totalColTotal = parseFloat(unComma(totalColTotal)) + parseFloat(unComma(RtotalVal.value));
            }
            document.getElementById("hidRecoverySum").value = parseFloat(document.getElementById("hidRecoverySum").value) + parseFloat(totalColrec);
            return totalColPaid;
        }
 
Share this answer
 
v2

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