Alex Gordon
Alex Gordon

Reputation: 60882

Getting the InnerHtml of an HTMLTable C#

This function is returning an HTML table:

private HtmlTable ConvertToHtml(DataTable dataTable)
        {
            var htmlTable = new HtmlTable();
            if (dataTable == null) return htmlTable; //null reference, empty table

            HtmlTableRow htmlRow = new HtmlTableRow();
            htmlTable.Rows.Add(htmlRow);

            foreach (DataColumn column in dataTable.Columns)
                htmlRow.Cells.Add(new HtmlTableCell() { InnerText = column.ColumnName });

            foreach (DataRow row in dataTable.Rows)
            {
                htmlRow = new HtmlTableRow();
                htmlTable.Rows.Add(htmlRow);

                foreach (DataColumn column in dataTable.Columns)
                    htmlRow.Cells.Add(new HtmlTableCell() { InnerText = row[column].ToString() });
            }

            return htmlTable;
        }

I would like to know how I can get the innerHTML of HtmlTable.

Currently, I am doing this:

        var bodyhtml = ConvertToHtml(r.tblSalesVolume);
        MessageBox.Show(bodyhtml.InnerHtml);

but it says that it doesn't have such a property.

How do I get the HTML code for the table?

Upvotes: 3

Views: 14927

Answers (3)

Terkel
Terkel

Reputation: 1575

You've written InnerText as the property, you should write InnerHtml. And there is no need for ToString() as HtmlTable.InnerHtml is a string.

See HtmlTable.InnerHtml


why don't you just create the html instead of using HtmlTable? instead of TableRow you just use before the "cell"-loop and after the "cell"-loop. and when you write a cell you just write ´value´.´There really isn't any reason to use HtmlTable to create the table html string.


private string ConvertToHtml(DataTable dataTable)
{
    StringBuilder sb = new StringBuilder();

    sb.Append("<table>");

    if (dataTable != null)
    {
        sb.Append("<tr>");

        foreach (DataColumn column in dataTable.Columns)
            sb.AppendFormat("<td>{0}</td>", HttpUtility.HtmlEncode(column.ColumnName));
        sb.Append("</tr>");

        foreach (DataRow row in dataTable.Rows)
        {
            sb.Append("<tr>");

            foreach (DataColumn column in dataTable.Columns)
                sb.AppendFormat("<td>{0}</td>", HttpUtility.HtmlEncode(row[column]));

            sb.Append("</tr>");
        }

    }

    sb.Append("</table>");
    return sb.ToString();
}

Upvotes: 1

Kash
Kash

Reputation: 9039

Based on Leon's answer, using HtmlTable.RenderControl() with OP's code:

StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);

var bodyhtml = ConvertToHtml(r.tblSalesVolume);

bodyhtml.RenderControl(hw);
MessageBox.Show(sb.ToString());

Upvotes: 1

Leon
Leon

Reputation: 3401

You can use HtmlTable.RenderControl() method.

Example:

StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);

HtmlTable.RenderControl(hw);
String HTMLContent = sb.ToString();

Upvotes: 8

Related Questions