GridView Calculations in Asp.Net



In this Post. GridView Calculations in Asp.Net using JavaScript

JavaScript Coding Used in This Example
----------------------------------------
<script type="text/javascript">
         function CalculateTotal()
         {
             var gv = document.getElementById("<%= GridView1.ClientID%>");
             var tb = gv.getElementsByTagName("input");
             var lb = gv.getElementsByTagName("span");
             var sub=0;
             var total=0;
          
             var indexp = 0;
             var indexq = 1;
             var price = 0;
             var qty = 0;
             var totalqty = 0;
             var tbcount = tb.length / 2;
             for (var i = 0; i < tbcount; i++)
             {
                 if(tb[i].type="text")
                 {
                     sub = parseFloat(tb[i + indexp].value) * parseFloat(tb[i + indexq].value);
                     if(isNaN(sub))
                     {
                         lb[i].innerHTML = "0.0";
                         sub = 0;
                     }
                     else
                     {
                         lb[i].innerHTML = sub;
                     }
                     if(isNaN(tb[i+indexp].value) || tb[i+indexq].value=="")
                     {
                         qty=0;
                     }
                     else
                     {
                         qty=tb[i+indexq].value;

                     }
                     totalqty=totalqty+parseInt(qty);
                     total=total+parseFloat(sub);
                 }
                 indexp++;
                 indexq++;

             }
             lb[tbcount].innerHTML = "Total Quantity "+totalqty;
             lb[tbcount+1].innerHTML = "Grand Total "+total;


         }
         </script>

----------------------------

PageLoad Coding
----------------------------

if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("sno", typeof(String)));
            dt.Columns.Add(new DataColumn("productname", typeof(String)));
            dt.Columns.Add(new DataColumn("quantity", typeof(String)));
            dt.Columns.Add(new DataColumn("price", typeof(String)));
            dt.Columns.Add(new DataColumn("subtotal", typeof(String)));
            dr = dt.NewRow();
            dr["sno"] = 1;
            dr["productname"] = "Laptop";
            dr["quantity"] = string.Empty;
           
            dr["price"] = string.Empty;
            dr["subtotal"] = "0.0";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["sno"] = 2;
            dr["productname"] = "Pen Drive";
            dr["quantity"] = string.Empty;
            
            dr["price"] = string.Empty;
            dr["subtotal"] = "0.0";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["sno"] = 3;
            dr["productname"] = "PC Mouse";
            dr["quantity"] = string.Empty;
           
            dr["price"] = string.Empty;
            dr["subtotal"] = "0.0";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["sno"] = 4;
            dr["productname"] = "Keyboard";
            dr["quantity"] = string.Empty;
           
            dr["price"] = string.Empty;
            dr["subtotal"] = "0.0";
            dt.Rows.Add(dr);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

0 Comment's

Comment Form