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.Display sum of Columns total in GridView Footer using Linq in ASP.Net<p>Hey <a class="username" rel="smile"> smile</a>,</p> <p>Please refer below sample it's working.</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;AdmissionNo&#34; HeaderText=&#34;Reg No&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Recievable&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lbl_Reci&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;Recievable&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;ItemStyle HorizontalAlign=&#34;Center&#34; /&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Arrears&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lbl_Arr&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;Arrears&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;ItemStyle HorizontalAlign=&#34;Center&#34; /&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;NetBal&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lbl_Total&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Bind(&#34;NetBal&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;ItemStyle HorizontalAlign=&#34;Center&#34; /&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</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) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;AdmissionNo&#34;, typeof(string)), new DataColumn(&#34;Recievable&#34;, typeof(decimal)), new DataColumn(&#34;Arrears&#34;, typeof(decimal)), new DataColumn(&#34;NetBal&#34;, typeof(decimal)) }); dt.Rows.Add(&#34;R-01&#34;, 500, 200, 700); dt.Rows.Add(&#34;R-02&#34;, 500, 200, 700); dt.Rows.Add(&#34;R-03&#34;, 500, 200, 700); dt.Rows.Add(&#34;R-04&#34;, 500, null, 500); dt.Rows.Add(&#34;R-05&#34;, 500, null, 500); dt.Rows.Add(&#34;R-06&#34;, 500, null, 500); if (dt.Rows.Count &gt; 0) { for (int i = 0; i &lt; dt.Rows.Count; i++) { for (int j = 0; j &lt; dt.Columns.Count; j++) { if (string.IsNullOrEmpty(dt.Rows[i][j].ToString())) { dt.Rows[i][j] = &#34;0&#34;; } } } this.GridView1.DataSource = dt; this.GridView1.DataBind(); decimal TotBal = dt.AsEnumerable().Sum(row =&gt; row.Field&lt;decimal&gt;(&#34;Recievable&#34;)); decimal Paid = dt.AsEnumerable().Sum(row =&gt; row.Field&lt;decimal&gt;(&#34;Arrears&#34;)); decimal Bal = dt.AsEnumerable().Sum(row =&gt; row.Field&lt;decimal&gt;(&#34;NetBal&#34;)); GridView1.FooterRow.Cells[0].Text = &#34;Total&#34;; GridView1.FooterRow.Cells[0].Font.Bold = true; GridView1.FooterRow.Cells[1].Text = TotBal.ToString(); GridView1.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Center; GridView1.FooterRow.Cells[1].Font.Bold = true; GridView1.FooterRow.Cells[1].BackColor = System.Drawing.Color.Honeydew; GridView1.FooterRow.Cells[2].Text = Paid.ToString(); GridView1.FooterRow.Cells[2].HorizontalAlign = HorizontalAlign.Center; GridView1.FooterRow.Cells[2].Font.Bold = true; GridView1.FooterRow.Cells[2].BackColor = System.Drawing.Color.Honeydew; GridView1.FooterRow.Cells[3].Text = Bal.ToString(); GridView1.FooterRow.Cells[3].HorizontalAlign = HorizontalAlign.Center; GridView1.FooterRow.Cells[3].Font.Bold = true; GridView1.FooterRow.Cells[3].BackColor = System.Drawing.Color.Honeydew; } } }</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 DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;AdmissionNo&#34;, GetType(String)), New DataColumn(&#34;Recievable&#34;, GetType(Decimal)), New DataColumn(&#34;Arrears&#34;, GetType(Decimal)), New DataColumn(&#34;NetBal&#34;, GetType(Decimal))}) dt.Rows.Add(&#34;R-01&#34;, 500, 200, 700) dt.Rows.Add(&#34;R-02&#34;, 500, 200, 700) dt.Rows.Add(&#34;R-03&#34;, 500, 200, 700) dt.Rows.Add(&#34;R-04&#34;, 500, Nothing, 500) dt.Rows.Add(&#34;R-05&#34;, 500, Nothing, 500) dt.Rows.Add(&#34;R-06&#34;, 500, Nothing, 500) If dt.Rows.Count &gt; 0 Then For i As Integer = 0 To dt.Rows.Count - 1 For j As Integer = 0 To dt.Columns.Count - 1 If String.IsNullOrEmpty(dt.Rows(i)(j).ToString()) Then dt.Rows(i)(j) = &#34;0&#34; End If Next Next Me.GridView1.DataSource = dt Me.GridView1.DataBind() Dim TotBal As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal)(&#34;Recievable&#34;)) Dim Paid As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal)(&#34;Arrears&#34;)) Dim Bal As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal)(&#34;NetBal&#34;)) GridView1.FooterRow.Cells(0).Text = &#34;Total&#34; GridView1.FooterRow.Cells(0).Font.Bold = True GridView1.FooterRow.Cells(1).Text = TotBal.ToString() GridView1.FooterRow.Cells(1).HorizontalAlign = HorizontalAlign.Center GridView1.FooterRow.Cells(1).Font.Bold = True GridView1.FooterRow.Cells(1).BackColor = System.Drawing.Color.Honeydew GridView1.FooterRow.Cells(2).Text = Paid.ToString() GridView1.FooterRow.Cells(2).HorizontalAlign = HorizontalAlign.Center GridView1.FooterRow.Cells(2).Font.Bold = True GridView1.FooterRow.Cells(2).BackColor = System.Drawing.Color.Honeydew GridView1.FooterRow.Cells(3).Text = Bal.ToString() GridView1.FooterRow.Cells(3).HorizontalAlign = HorizontalAlign.Center GridView1.FooterRow.Cells(3).Font.Bold = True GridView1.FooterRow.Cells(3).BackColor = System.Drawing.Color.Honeydew End If End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/CimW6JP.jpg" alt="" width="277" height="359" /></p>https://www.aspforums.net:443/Threads/619096/Display-sum-of-Columns-total-in-GridView-Footer-using-Linq-in-ASPNet/https://www.aspforums.net:443/Threads/619096/Display-sum-of-Columns-total-in-GridView-Footer-using-Linq-in-ASPNet/Thu, 13 Dec 2018 22:29:39 GMT