Click here to Skip to main content
15,890,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating a DataTable dynamically and would like the contents of some cells to be html.

I think the below explains what I would like to do...
DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("DummyColumnName"));
        dt.Rows.Add(12);
        dt.Rows.Add(13);
        dt.Rows.Add(11);
        dt.Rows.Add("<div>dummy text</div>");
        myGridView.DataSource = dt;
        myGridView.DataBind();




The html page will display the html tag string <div>dummy text</div> in the cell rather than the html itself
Posted

So, what are you asking? How to get the HTML text into your datatable? If you have placed HTML in your datatable, are you having trouble rendering it on a web control?
 
Share this answer
 
XML
Yes thats it, my current html (looking only at the specific cell) is written as this...


<td>&lt;div&gt;dummy text&lt;/div&gt;</td>


And what I would want is

<td><div>dummy text</div></td>


Maybe not possible with DataTable and GridView...
 
Share this answer
 
I found a solution...

You start listening for the GridViews RowDataBound event which fires once for each row added to the GridView from the source. Then call the Server.HtmlDecode method and pass the string and the job is done...

Example:

<pre lang="cs">protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = Server.HtmlDecode(e.Row.Cells[0].Text);
        }

    }


 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900