How To Add client side calculation for gridview column

In this post, I will show you how to calculate the column sum of GridView using javaScript.

<%@ Page Language="C#" AutoEventWireup="true"  
Inherits="GridViewClientSideCalculation" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
<html xmlns="">  
<head runat="server">  

<script type="text/javascript">  
function CalcSellPrice2(CurrentPrice, DiscountCtrlID, CustomerCtrlID)  
var Discount= parseFloat(document.getElementById(DiscountCtrlID).value);  
var SellPrice=document.getElementById(CustomerCtrlID);  
var SellPriceValue = parseFloat(CurrentPrice -((CurrentPrice*Discount)/100));  
//var SellPriceValueRound = Math.round(SellPriceValue,4);  
var SellPriceValueRound = SellPriceValue;  
SellPrice.innerHTML= SellPriceValueRound ;  



<form id="form1" runat="server">  
      <asp:GridView ID="GridView1" runat="server"  
AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">  
              <asp:TemplateField HeaderText="List Price" Visible="True">  
                      <asp:Label ID="lblCurrentListPrice" runat="server"  
Text='<%# Eval("Price") %>'></asp:Label>  
              <asp:TemplateField HeaderText="Discount">  
                      <asp:TextBox ID="txtDiscountOnItem" Width="100px"  
runat="Server" />  
              <asp:TemplateField HeaderText="Sell Price">  
                      <asp:Label ID="lblSellPrice" runat="Server" />  
using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
public partial class GridViewClientSideCalculation : System.Web.UI.Page  
 protected void Page_Load(object sender, EventArgs e)  
     GridView1.DataSource = GetData();  
 public DataSet GetData()  
     DataSet ds = new DataSet();  
     DataTable dt = new DataTable("Product");  
     DataRow dr;  
     dt.Columns.Add(new DataColumn("Price", typeof(Int32)));  
     dt.Columns.Add(new DataColumn("DisCount", typeof(Int32)));  
     dt.Columns.Add(new DataColumn("SellPrice", typeof(Int32)));  
     for (int i = 1; i <= 10; i++)  
         dr = dt.NewRow();  
         dr[0] = i;  
         dr[1] = i * 2;  
         dr[2] = 1 * 3;  
     Session["dt"] = dt;  
     return ds;  
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
     if (e.Row.RowType == DataControlRowType.DataRow)  
         Label lblCurrentListPrice = (Label)e.Row.FindControl("lblCurrentListPrice");  
         Double CurPr = Convert.ToDouble(lblCurrentListPrice.Text);  
         TextBox txtDiscountOnItem = (TextBox)e.Row.FindControl("txtDiscountOnItem");  
         Label lblSellPrice = (Label)e.Row.FindControl("lblSellPrice");  
"CalcSellPrice2(" + CurPr + ", '" + txtDiscountOnItem.ClientID + "','"  
+ lblSellPrice.ClientID + "')");  

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads