core core - 1 year ago 65
ASP.NET (C#) Question

How to render decoded HTML in a (i.e. a <br>) in GridView cell

I'm binding a GridView to an LINQ query. Some of the fields in the objects created by the LINQ statement are strings, and need to contain new lines.

Apparently, GridView HTML-encodes everything in each cell, so I can't insert a <br /> to create a new line within a cell.

How do I tell GridView not to HTML encode the contents of cells?

Maybe I should use a different control instead?

Answer Source

Can you subscribe to the RowDataBound event? If you can, you can run:

if (e.Row.RowType == DataControlRowType.DataRow)
  string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[0].Text);
  e.Row.Cells[0].Text = decodedText;