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 ASP.Net DataList Grand Total in footer using C# and VB.Net<p>Hi <a class="username" rel="VinitaAkha">VinitaAkha</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DataList ID=&#34;dlCustomers&#34; runat=&#34;server&#34; RepeatColumns=&#34;2&#34; CellPadding=&#34;5&#34; Font-Size=&#34;20px&#34; OnItemDataBound=&#34;dlCustomers_ItemDataBound&#34;&gt; &lt;ItemTemplate&gt; &lt;table class=&#34;table&#34; border=&#34;0&#34; style=&#34;width: 250px; height: 350px; font-size: 20px&#34;&gt; &lt;tr&gt; &lt;th colspan=&#34;3&#34; align=&#34;left&#34;&gt; &lt;asp:CheckBox ID=&#34;chkRow&#34; runat=&#34;server&#34; Font-Bold=&#34;true&#34; Text=&#39;&lt;%# Eval(&#34;Name&#34;)%&gt;&#39; AutoPostBack=&#34;true&#34; OnCheckedChanged=&#34;chkRow_CheckedChanged&#34; /&gt; &lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Farmer Id: &lt;/td&gt; &lt;td&gt; &lt;asp:Label Text=&#39;&lt;%#Eval(&#34;Id&#34;)%&gt;&#39; runat=&#34;server&#34; ID=&#34;lblId&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Name: &lt;/td&gt; &lt;td&gt; &lt;asp:Label Text=&#39;&lt;%#Eval(&#34;Name&#34;)%&gt;&#39; runat=&#34;server&#34; ID=&#34;Name&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Potato Quantity: &lt;/td&gt; &lt;td&gt; &lt;asp:Label Text=&#39;&lt;%#Eval(&#34;PQuantity&#34;)%&gt;&#39; runat=&#34;server&#34; ID=&#34;PQuantity&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Company Potato Quantity per Ton: &lt;/td&gt; &lt;td&gt; &lt;asp:Label Text=&#39;&lt;%#Eval(&#34;PoQuantity&#34;)%&gt;&#39; runat=&#34;server&#34; ID=&#34;PoQuantity&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Total: &lt;/td&gt; &lt;td&gt; &lt;asp:Label runat=&#34;server&#34; ID=&#34;Total&#34; /&gt; &lt;asp:Label ID=&#34;lblGrandTotal&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;asp:Label runat=&#34;server&#34; ID=&#34;lblMsg&#34; ForeColor=&#34;Red&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&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;Id&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;PQuantity&#34;, typeof(int)), new DataColumn(&#34;PoQuantity&#34;, typeof(int)) }); dt.Rows.Add(1, &#34;Ross&#34;, 10, 20); dt.Rows.Add(2, &#34;Monica&#34;, 20, 30); dlCustomers.DataSource = dt; dlCustomers.DataBind(); } } protected void chkRow_CheckedChanged(object sender, EventArgs e) { decimal total = 0; CheckBox chk = (sender) as CheckBox; DataListItem item = (DataListItem)chk.NamingContainer; Label id = (Label)item.FindControl(&#34;lblId&#34;); Label Name = (Label)item.FindControl(&#34;Name&#34;); Label PQuantity = (Label)item.FindControl(&#34;PQuantity&#34;); Label PoQuantity = (Label)item.FindControl(&#34;PoQuantity&#34;); Label Total = (Label)item.FindControl(&#34;Total&#34;); total += Convert.ToDecimal(PQuantity.Text) + Convert.ToDecimal(PoQuantity.Text); Label message = (Label)item.FindControl(&#34;lblMsg&#34;); if (chk.Checked == true) { message.Text = &#34;Id = &#34; + id.Text + &#34;&lt;br/&gt;&#34; + &#34;Name = &#34; + Name.Text + &#34;PQuantity = &#34; + PQuantity.Text + &#34;PoQuantity = &#34; + PoQuantity.Text + &#34;Total = &#34; + Total.Text; } else if (chk.Checked == false) { message.Text = string.Empty; } (item.FindControl(&#34;lblGrandTotal&#34;) as Label).Text = total.ToString(); } protected void dlCustomers_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Label PQuantity = (Label)e.Item.FindControl(&#34;PQuantity&#34;); Label PoQuantity = (Label)e.Item.FindControl(&#34;PoQuantity&#34;); Label Total = (Label)e.Item.FindControl(&#34;Total&#34;); Total.Text = (Convert.ToDecimal(PQuantity.Text) + Convert.ToDecimal(PoQuantity.Text)).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 DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;PQuantity&#34;, GetType(Integer)), New DataColumn(&#34;PoQuantity&#34;, GetType(Integer))}) dt.Rows.Add(1, &#34;Ross&#34;, 10, 20) dt.Rows.Add(2, &#34;Monica&#34;, 20, 30) dlCustomers.DataSource = dt dlCustomers.DataBind() End If End Sub Protected Sub chkRow_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Dim total As Decimal = 0 Dim chk As CheckBox = TryCast((sender), CheckBox) Dim item As DataListItem = CType(chk.NamingContainer, DataListItem) Dim id As Label = CType(item.FindControl(&#34;lblId&#34;), Label) Dim Name As Label = CType(item.FindControl(&#34;Name&#34;), Label) Dim PQuantity As Label = CType(item.FindControl(&#34;PQuantity&#34;), Label) Dim PoQuantity As Label = CType(item.FindControl(&#34;PoQuantity&#34;), Label) Dim Total As Label = CType(item.FindControl(&#34;Total&#34;), Label) total += Convert.ToDecimal(PQuantity.Text) + Convert.ToDecimal(PoQuantity.Text) Dim message As Label = CType(item.FindControl(&#34;lblMsg&#34;), Label) If chk.Checked = True Then message.Text = &#34;Id = &#34; &amp; id.Text &amp; &#34;&lt;br/&gt;&#34; &amp; &#34;Name = &#34; + Name.Text &amp; &#34;PQuantity = &#34; + PQuantity.Text &amp; &#34;PoQuantity = &#34; + PoQuantity.Text &amp; &#34;Total = &#34; + Total.Text ElseIf chk.Checked = False Then message.Text = String.Empty End If (TryCast(item.FindControl(&#34;lblGrandTotal&#34;), Label)).Text = total.ToString() End Sub Protected Sub dlCustomers_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs) If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then Dim PQuantity As Label = CType(e.Item.FindControl(&#34;PQuantity&#34;), Label) Dim PoQuantity As Label = CType(e.Item.FindControl(&#34;PoQuantity&#34;), Label) Dim Total As Label = CType(e.Item.FindControl(&#34;Total&#34;), Label) Total.Text = (Convert.ToDecimal(PQuantity.Text) + Convert.ToDecimal(PoQuantity.Text)).ToString() End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/jLWl7bR.gif" alt="" width="331" height="402" /></p>https://www.aspforums.net:443/Threads/119947/Display-ASPNet-DataList-Grand-Total-in-footer-using-C-and-VBNet/https://www.aspforums.net:443/Threads/119947/Display-ASPNet-DataList-Grand-Total-in-footer-using-C-and-VBNet/Thu, 22 Aug 2019 06:41:16 GMT