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?
What about setting the
HtmlEncode
property tofalse
? To me, this is much simpler.I got around this by first inserting the data into my sql-server table from a multi-line textbox using
Then I used Ray Booysen's solution to return it to my gridview:
You have to bind to the the DataBoundGrid event and change the Rendering for the columns you want to render HTML code.
@Ray Booysen
answers is right but in some cases HtmlDecode() can't handle your problem. you can use UrlDecode() instead of HtmlDecode().here is another solution:
Are normal newlines preserved in output? If so, you can send the newlines, and use the css style
white-space: pre
, which would preserve newlines, spaces and tabs.