Add Sub Total to Each Group of HTML Table Row using C# in ASP.Net

Last Reply 10 months ago By arunkurmi

Posted 10 months ago

I want to do Sub Total of HTML Rows in while loop after a group.

{
    DataBody = @"<td colspan='8'>
                <table cellpadding='0' cellspacing='0' border='0' width='100%' style='font-family: arial;padding: 3px 4px;font-size: 10pt;text-align: left;'><thead>
                <tr bgcolor='#86BC24' style='border-collapse: collapse;font-family: Arial; font-size: 10pt; font-weight:bold; line-height:22px; color:white; height:22pt;text-align: center;'><td style='border: 1px solid #AAAAAA;'></td><td style='border: 1px solid #AAAAAA;'></td><td colspan='2' style='background-color: #2F74B5;border: 1px solid #AAAAAA;'>Prev Month Wins</td><td colspan='2' style='background-color:#2F74B5;border: 1px solid #AAAAAA;'>YTD Wins</td><td style='background-color:#538234;border: 1px solid #AAAAAA;' colspan='2'>Pipeline : Net Additions <br/> (Prev Month)</td><td style='background-color:#538234;border: 1px solid #AAAAAA;' colspan='2'>YTD Pipeline</td><td style='background-color:#538234;border: 1px solid #AAAAAA;' colspan='2'>YTD Late Stage</td><td style= 'background-color:#C55911;border: 1px solid #AAAAAA;' colspan ='2'>YTD Losses</td><td style= 'background-color:#C55911;border: 1px solid #AAAAAA;' colspan ='2'>YTD On Hold</td><td style='background-color:#03AFF1;border: 1px solid #AAAAAA;' >&nbsp;</td></tr><tr bgcolor='#86BC24' style='border-collapse: collapse;font-family: Arial; font-size: 10pt; font-weight:bold; line-height:22px; height:22pt;text-align: center;'><td style='color: white;border: 1px solid #AAAAAA;text-align: left;'>Partner Service Line</td><td style='color: white;border: 1px solid #AAAAAA;text-align: left;'>Partner Name</td><td style='background-color:#9BC1E6;border: 1px solid #AAAAAA;'>Count</td><td style='background-color:#9BC1E6;border: 1px solid #AAAAAA;'>Value</td><td style='background-color:#9BC1E6;border: 1px solid #AAAAAA;'>Count</td><td style='background-color:#9BC1E6;border: 1px solid #AAAAAA;'>Value</td><td style='background-color:#A9D08F;border: 1px solid #AAAAAA;'>Count</td><td style='background-color:#A9D08F;border: 1px solid #AAAAAA;'>Value</td><td style='background-color:#A9D08F;border: 1px solid #AAAAAA;'>Count</td><td style='background-color:#A9D08F;border: 1px solid #AAAAAA;'>Value</td><td style='background-color:#A9D08F;border: 1px solid #AAAAAA;'>Count</td><td style='background-color:#A9D08F;border: 1px solid #AAAAAA;'>Value</td><td style='background-color:#F4AF86;border: 1px solid #AAAAAA;'>Count</td><td style='background-color:#F4AF86;border: 1px solid #AAAAAA;'>Value</td><td style='background-color:#F4AF86;border: 1px solid #AAAAAA;'>Count</td><td style='background-color:#F4AF86;border: 1px solid #AAAAAA;'>Value</td><td style='background-color:#03AFF1;color: white;'>Win Ratio %</br> (By Value)</td></tr></thead>";

    while (dsDailyEM.Tables[0].Rows.Count - 1 >= ictr)
    {
        DataBody = DataBody + "<tr><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: left;'>" + dsDailyEM.Tables[0].Rows[ictr]["ServiceLine"] + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: left;'>" + dsDailyEM.Tables[0].Rows[ictr]["PartnerName"] + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align:center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["CountMonthlyWins"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align:center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["ValueMonthlyWins"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align:center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["YTDCountWins"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["YTDSumWins"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["PipelineMonthlyOpenCount"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["PipeLineMonthlyOpenValue"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["TotalPipelineCount"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["TotalPipeLineValue"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align:center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["LateStageCount"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["LateStageValue"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["YTDLossesCount"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["YTDLossesValue"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["OnHoldCount"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["OnHoldValue"]), 2) + "</td><td style='border: 1px solid #AAAAAA;padding: 3px 4px;font-size: 10pt;text-align: center;'>" + Math.Round(Convert.ToDouble(dsDailyEM.Tables[0].Rows[ictr]["winRatio"]), 2) + " %</td></tr>";
        if (dsDailyEM.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < dsDailyEM.Tables[0].Rows.Count - 1; i++)
            {
                if (i > 0)
                {
                    if (Convert.ToString(dsDailyEM.Tables[0].Rows[ictr]["ServiceLine"]) != Convert.ToString(dsDailyEM.Tables[0].Rows[ictr - 1]["ServiceLine"]))
                    {
                        DataBody = DataBody + "<tr style='font - weight: 700;background-color: lemonchiffon;'><td colspan='17'></td></tr>";
                    }
                }
            }
        }

        ictr = ictr + 1;
    }
}
Posted 10 months ago

Hi iammann,

check this example now please take its reference and correct your code.

HTML

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

Namespaces

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dtProducts = new DataTable();
        dtProducts.Columns.Add("OrderID", typeof(int));
        dtProducts.Columns.Add("ProductName", typeof(string));
        dtProducts.Columns.Add("Price", typeof(double));
        dtProducts.Rows.Add(1001, "Apple", 100.00);
        dtProducts.Rows.Add(1001, "Orange", 50.00);
        dtProducts.Rows.Add(1002, "Banana", 40.00);
        dtProducts.Rows.Add(1003, "Grapes", 110.00);
        dtProducts.Rows.Add(1003, "Litche", 210.00);
        dtProducts.Rows.Add(1004, "Mango", 120.00);
        double total = 0;
        string table = "<table><tr><th>OrderID</th><th>ProductName</th><th>Price</th></tr>";
        var result = from dr in dtProducts.AsEnumerable()
                        group dr by dr["OrderID"] into cg
                        select new
                        {
                            row = cg.CopyToDataTable(),
                            sum = cg.Sum(dr => Convert.ToDouble(dr["Price"]))
                        };
        foreach (var item in result)
        {
            DataTable dt = item.row;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                table += "<tr><td>" + dt.Rows[i]["OrderID"] + "</td><td>" + dt.Rows[i]["ProductName"] + "</td><td>" + dt.Rows[i]["Price"] + "</td><tr>";
            }
            table += "<tr><td></td><td><b>Sub Total</b></td><td><b>" + item.sum + "</b></td><tr>";
            total += item.sum;
        }
        table += "<tr><td></td><td><b>Total</b></td><td><b>" + total + "</b></td><tr>";
        table += "</table>";
        lblTable.Text = table;
    }
}

Screenshot