ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Calculate ASP.Net GridView Running and Grand Total using JavaScript<p>Hi <a class="username" rel="Sadia.net">Sadia.net</a>,</p> <p>I have simplified your code.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;GridView Running and Grand Total JavaScript&lt;/title&gt; &lt;script type=&#34;text/javascript&#34;&gt; function CalculateTotals() { var gv = document.getElementById(&#34;&lt;%= myGridView.ClientID %&gt;&#34;); var rows = gv.getElementsByTagName(&#34;tr&#34;); var grandTotal = 0; for (var i = 0; i &lt; rows.length; i++) { var tds = rows[i].getElementsByTagName(&#34;td&#34;); if (rows[i].getElementsByTagName(&#34;td&#34;).length &gt; 0) { var price = rows[i].getElementsByTagName(&#34;span&#34;)[2].innerHTML; var quantity = rows[i].getElementsByTagName(&#34;input&#34;)[0].value; var total = parseFloat(price) * parseFloat(quantity); rows[i].getElementsByTagName(&#34;span&#34;)[3].innerHTML = isNaN(total) ? 0 : total; grandTotal += isNaN(total) ? 0 : total; } } document.getElementById(&#34;&lt;%= lblSumTotal.ClientID %&gt;&#34;).innerHTML = grandTotal; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;asp:Label ID=&#34;Cart&#34; runat=&#34;server&#34; Font-Size=&#34;X-Large&#34; Text=&#34;Your Cart&#34;&gt;&lt;/asp:Label&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;myGridView&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;ProductID&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblProductID&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;ProductID&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;ProductName&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblProductName&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;ProductName&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Price&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblPrice&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;Price&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Quantity&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtQty&#34; runat=&#34;server&#34; onkeyup=&#34;CalculateTotals();&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Total&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblTotal&#34; runat=&#34;server&#34; Text=&#34;0&#34;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Delete&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&#34;lnkDelete&#34; runat=&#34;server&#34;&gt;Delete&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; Sum Total : &lt;asp:Label ID=&#34;lblSumTotal&#34; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.AddRange(new System.Data.DataColumn[] { new System.Data.DataColumn(&#34;ProductID&#34;, typeof(int)), new System.Data.DataColumn(&#34;ProductName&#34;, typeof(string)), new System.Data.DataColumn(&#34;Price&#34;,typeof(int)) }); dt.Rows.Add(1, &#34;Product 1&#34;, 10); dt.Rows.Add(2, &#34;Product 2&#34;, 12); dt.Rows.Add(3, &#34;Product 3&#34;, 9); dt.Rows.Add(4, &#34;Product 4&#34;, 15); myGridView.DataSource = dt; myGridView.DataBind(); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then Dim dt As System.Data.DataTable = New System.Data.DataTable() dt.Columns.AddRange(New System.Data.DataColumn() { New System.Data.DataColumn(&#34;ProductID&#34;, GetType(Integer)), New System.Data.DataColumn(&#34;ProductName&#34;, GetType(String)), New System.Data.DataColumn(&#34;Price&#34;, GetType(Integer))}) dt.Rows.Add(1, &#34;Product 1&#34;, 10) dt.Rows.Add(2, &#34;Product 2&#34;, 12) dt.Rows.Add(3, &#34;Product 3&#34;, 9) dt.Rows.Add(4, &#34;Product 4&#34;, 15) myGridView.DataSource = dt myGridView.DataBind() End If End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/G2zMlw2.jpg" alt="" width="472" height="219" /></p>https://www.aspforums.net:443/Threads/136474/Calculate-ASPNet-GridView-Running-and-Grand-Total-using-JavaScript/https://www.aspforums.net:443/Threads/136474/Calculate-ASPNet-GridView-Running-and-Grand-Total-using-JavaScript/Fri, 16 Aug 2019 02:25:34 GMT