HTML generate rowspan in html table string

c# html html-agility-pack html-table

Question

I have the following code, which adds rowspan to a datatable in an html string, but I want it not to receive as a parameter a datatable, but instead to receive as a parameter an html string in C#.

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Subject");
    dt.Columns.Add("Code");
    dt.Columns.Add("Test");
    dt.Columns.Add("Class");
    dt.Rows.Add("Math", "9", "ABC", "D1");
    dt.Rows.Add("Math", "9", "ABD", "D2");
    dt.Rows.Add("Math", "9", "ABE", "D3");
    dt.Rows.Add("Math", "9", "ABF", "D4");
    dt.Rows.Add("Science", "91", "ABG", "D1");
    dt.Rows.Add("Science", "91", "ABH", "D2");
    dt.Rows.Add("Science", "91", "ABI", "D3");
    dt.Rows.Add("English", "191", "ABJ", "D1");
    Label1.Text = ConvertDataTableToHTML(dt);

}
public static string ConvertDataTableToHTML(DataTable dt)
{
    string html = "<table border=1>";
    //add header row
    html += "<tr>";
    for (int i = 0; i < dt.Columns.Count; i++)
    {
        html += "<td>" + dt.Columns[i].ColumnName + "</td>";
    }
    html += "</tr>";
    //add rows
    string sub = "";
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        html += "<tr>";
        int count = dt.Select("Subject ='" + dt.Rows[i][0].ToString() + "'").Count();
        for (int j = 0; j < dt.Columns.Count; j++)
        {
            if (j < 2)
            {
                if (sub != dt.Rows[i][0].ToString())
                {
                    html += "<td rowspan='" + count + "'>" + dt.Rows[i][j].ToString() + "</td>";
                }
                continue;
            }
            html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
        }
        sub = dt.Rows[i][0].ToString();
        html += "</tr>";
    }
    html += "</table>";
    return html;
}

This is the result that I hope

expected result

Popular Answer

There are a few things wrong with the code, and a few ways you can improve your code:

  1. <table border=1> is missing the quotes around the 1. It should be <table border='1'>.
  2. You are missing the <thead> and <tbody> tags around the row tags.
  3. Instead of constantly using +=, use a StringBuilder.
  4. To make your code cleaner, you can change some of your for loops to foreach loops.

Your code should be changed to something like this:

public static string ConvertDataTableToHTML(DataTable dt)
{
    StringBuilder builder = new StringBuilder();

    //add header row
    builder.Append("<table border='1'><thead><tr>");
    foreach (DataColumn col in dt.Columns)
    {
        builder.Append("<td>");
        builder.Append(col.ColumnName);
        builder.Append("</td>");
    }

    builder.Append("</tr></thead><tbody>");

    //add rows
    string sub = "";

    ...

    builder.Append("</tbody></table>");

    return builder.ToString();
}

(Just apply the same concepts to the middle code that I removed.)



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why