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 row Total and Grand total on Button Click using C# and VB.Net<p>Hi <a class="username" rel="rajeesh"> rajeesh</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; ShowFooter=&#34;True&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;itemname&#34; HeaderText=&#34;Item Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;price&#34; HeaderText=&#34;Price&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Quantity&#34;&gt; &lt;FooterTemplate&gt; &lt;asp:Label ID=&#34;Label44&#34; runat=&#34;server&#34; Text=&#34;Sub Total&#34;&gt;&lt;/asp:Label&gt; &lt;/FooterTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtQuantity&#34; runat=&#34;server&#34; Height=&#34;30px&#34; Text=&#39;&lt;%# Eval(&#34;obqty&#34;) %&gt;&#39; Width=&#34;40px&#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;FooterTemplate&gt; &lt;asp:Label ID=&#34;lblGrandTotal&#34; runat=&#34;server&#34; Text=&#34;0&#34;&gt;&lt;/asp:Label&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:Button Text=&#34;Calculate&#34; runat=&#34;server&#34; OnClick=&#34;Button3_Click&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.AddRange(new System.Data.DataColumn[] { new System.Data.DataColumn(&#34;itemname&#34;, typeof(string)), new System.Data.DataColumn(&#34;obqty&#34;, typeof(int)), new System.Data.DataColumn(&#34;price&#34;, typeof(int))}); dt.Rows.Add(&#34;Pen&#34;, 10, 100); dt.Rows.Add(&#34;Pencil&#34;, 20, 100); dt.Rows.Add(&#34;Eraser&#34;, 30, 100); dt.Rows.Add(&#34;Highlighter&#34;, 40, 100); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void Button3_Click(object sender, EventArgs e) { decimal grandTotal = 0; foreach (GridViewRow row in GridView1.Rows) { decimal quantity = Convert.ToDecimal((row.FindControl(&#34;txtQuantity&#34;) as TextBox).Text); decimal price = Convert.ToDecimal(row.Cells[1].Text); decimal total = 0; if (row.RowType == DataControlRowType.DataRow) { total = price * quantity; (row.FindControl(&#34;lblTotal&#34;) as Label).Text = total.ToString(); } grandTotal += total; } (GridView1.FooterRow.FindControl(&#34;lblGrandTotal&#34;) as Label).Text = grandTotal.ToString(); }</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 Me.IsPostBack Then Dim dt As System.Data.DataTable = New System.Data.DataTable() dt.Columns.AddRange(New System.Data.DataColumn() {New System.Data.DataColumn(&#34;itemname&#34;, GetType(String)), New System.Data.DataColumn(&#34;obqty&#34;, GetType(Integer)), New System.Data.DataColumn(&#34;price&#34;, GetType(Integer))}) dt.Rows.Add(&#34;Pen&#34;, 10, 100) dt.Rows.Add(&#34;Pencil&#34;, 20, 100) dt.Rows.Add(&#34;Eraser&#34;, 30, 100) dt.Rows.Add(&#34;Highlighter&#34;, 40, 100) GridView1.DataSource = dt GridView1.DataBind() End If End Sub Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Dim grandTotal As Decimal = 0 For Each row As GridViewRow In GridView1.Rows Dim quantity As Decimal = Convert.ToDecimal((TryCast(row.FindControl(&#34;txtQuantity&#34;), TextBox)).Text) Dim price As Decimal = Convert.ToDecimal(row.Cells(1).Text) Dim total As Decimal = 0 If row.RowType = DataControlRowType.DataRow Then total = price * quantity TryCast(row.FindControl(&#34;lblTotal&#34;), Label).Text = total.ToString() End If grandTotal += total Next TryCast(GridView1.FooterRow.FindControl(&#34;lblGrandTotal&#34;), Label).Text = grandTotal.ToString() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/Zk7grkn.gif" alt="" width="282" height="396" /></p>https://www.aspforums.net:443/Threads/787198/Calculate-ASPNet-GridView-row-Total-and-Grand-total-on-Button-Click-using-C-and-VBNet/https://www.aspforums.net:443/Threads/787198/Calculate-ASPNet-GridView-row-Total-and-Grand-total-on-Button-Click-using-C-and-VBNet/Wed, 07 Aug 2019 01:32:43 GMT