HTML generate rowspan in html table string

c# html html-agility-pack html-table

Question

I have the following C# code that adds rowspan to a datatable in an html string, but I would want for it to take an html text as a parameter rather than a datatable.

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 outcome I'm hoping for.

expected result

1
0
8/28/2017 9:09:02 PM

Popular Answer

There are some issues with the code, and there are several fixes you can make:

  1. <table border=1> lacks the quotation marks surrounding the1 It ought to be<table border='1'> .
  2. You don't have the<thead> and <tbody> surrounding the row tags with tags.
  3. Rather than continuously utilizing+= , use aStringBuilder .
  4. You may alter parts of your code to make it cleaner.for cycles toforeach loops.

You need update your code to read 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();
}

(Use the same principles to replace the center of the code that I deleted.)

0
8/29/2017 12:09:58 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow