Display sum of HTML Table column in footer using C# in ASP.Net

ramco1917
 
on Aug 05, 2022 10:36 PM
556 Views

Hi Price

I am trying like this but it is not displaying total.

Display sum of HTML Table column in footer using jQuery in ASP.Net

tot = tot + Convert.ToDecimal(colum.Amount);
htmlTable.Append("<tfoot><tr><td colspan='9' style='text-align:center'>Total</td><td style='text-align:right' id='lblSum'" + tot +"></td></tr></tfoot>");

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
KasimA
 
on Aug 06, 2022 01:12 AM

Hi ramco1917,

Please refer below sample. 

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<asp:Label ID="lblHtml" runat="server" />

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        var trs = $("#tbldata tbody").find("tr");
        var sum = 0;
        $(trs).each(function () {
            var tds = $(this).find('td');
            sum = sum + parsefloat($(tds).eq(2).html());
            alert(parsefloat($(tds).eq(2).html()));
        });
        $("#lblsum").html(sum);
    })
</script>

Namespace

C#

using System.Text;

VB.Net

Imports System.Text

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.GetData();
    }
}

private void GetData()
{
    try
    {
        using (NORTHWINDEntities entities = new NORTHWINDEntities())
        {
            StringBuilder htmlTable = new StringBuilder();
            int rownum = 0;
            var Result = (from customer in entities.Orders.Take(5)
                            select customer).ToList();
            if (Result != null)
            {
                htmlTable.Append("<table class='table table-striped table-hover dataTable table-bordered' data-provide='data-table' id='tbldata'>");
                htmlTable.Append("<thead><tr><th>ID</th><th>Year</th><th style='text-align:right'>Budget Amount</th</tr></thead>");
                htmlTable.Append("<tbody>");
                decimal tot = 0;
                foreach (var colum in Result)
                {
                    rownum++;
                    htmlTable.Append("<tr>");
                    htmlTable.Append("<td class='ID'>" + rownum + " <span style='display:none'>" + colum.CustomerID + "</span></td>");
                    htmlTable.Append("<td>" + colum.OrderDate.Value.Year + "</td>");
                    htmlTable.Append("<td style='text-align:right'>" + Convert.ToDecimal(colum.Freight).ToString("0,0", System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN")) + "</td>");
                    htmlTable.Append("</tr>");
                    tot += Convert.ToDecimal(colum.Freight);
                }
                htmlTable.Append("</tbody>");
                htmlTable.Append("<tfoot><tr><td colspan='2' style='text-align:center'>Total</td><td style='text-align:right' id='lblSum'>" + tot.ToString("0,0", System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN")) + "</td></tr></tfoot>");
                htmlTable.Append("</table>");
                lblHtml.Text = htmlTable.ToString();
            }
        }
    }
    catch (Exception ex)
    {
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.GetData()
    End If
End Sub

Private Sub GetData()
    Try
        Using entities As NORTHWINDEntities = New NORTHWINDEntities()
            Dim htmlTable As StringBuilder = New StringBuilder()
            Dim rownum As Integer = 0
            Dim Result = (From customer In entities.Orders.Take(5) Select customer).ToList()
            If Result IsNot Nothing Then
                htmlTable.Append("<table class='table table-striped table-hover dataTable table-bordered' data-provide='data-table' id='tbldata'>")
                htmlTable.Append("<thead><tr><th>ID</th><th>Year</th><th style='text-align:right'>Budget Amount</th</tr></thead>")
                htmlTable.Append("<tbody>")
                Dim tot As Decimal = 0

                For Each colum In Result
                    rownum += 1
                    htmlTable.Append("<tr>")
                    htmlTable.Append("<td class='ID'>" & rownum & " <span style='display:none'>" & colum.CustomerID & "</span></td>")
                    htmlTable.Append("<td>" & colum.OrderDate.Value.Year & "</td>")
                    htmlTable.Append("<td style='text-align:right'>" & Convert.ToDecimal(colum.Freight).ToString("0,0", System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN")) & "</td>")
                    htmlTable.Append("</tr>")
                    tot += Convert.ToDecimal(colum.Freight)
                Next

                htmlTable.Append("</tbody>")
                htmlTable.Append("<tfoot><tr><td colspan='2' style='text-align:center'>Total</td><td style='text-align:right' id='lblSum'>" & tot.ToString("0,0", System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN")) & "</td></tr></tfoot>")
                htmlTable.Append("</table>")
                lblHtml.Text = htmlTable.ToString()
            End If
        End Using

    Catch ex As Exception
    End Try
End Sub

Screenshot