30

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?

YOU
  • 120,166
  • 34
  • 186
  • 219
core
  • 32,451
  • 45
  • 138
  • 193

9 Answers9

50

What about setting the HtmlEncode property to false? To me, this is much simpler.

<asp:BoundField DataField="MyColumn" HtmlEncode="False" />
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Aaron Daniels
  • 9,563
  • 6
  • 45
  • 58
  • 4
    Agreed. This is much much easier. – willem Mar 18 '11 at 12:32
  • At my end, its adding a new column with blank header and original one is still there. so its of no use... – Adarsh Rajput Sep 17 '14 at 05:59
  • 1
    Found a thing that you also need to set `autogeneratecolumns="false"` attribute to GridView. But in this case you need to add `` for each database field inside `` sub-tag of GridView control. – Adarsh Rajput Sep 17 '14 at 06:03
47

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;
}
Ray Booysen
  • 28,894
  • 13
  • 84
  • 111
  • One of the advantages of this method over setting `HtmlEncode` property on the `BoundField` to `false` is that you can add HTML tags around the text and still use the HTML encoding of the data. For instance, `e.Row.Cells[0].Text = "" + e.Row.Cells[0].Text + "";` – MÇT Dec 19 '14 at 07:04
4
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    for (int i = 0; i < e.Row.Cells.Count; i++)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[i].Text);
            e.Row.Cells[i].Text = decodedText;
        }
    }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Developer
  • 41
  • 1
4

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.

configurator
  • 40,828
  • 14
  • 81
  • 115
2

I got around this by first inserting the data into my sql-server table from a multi-line textbox using

   replace (txt = Replace(txt, vbCrLf,"<br />"))

Then I used Ray Booysen's solution to return it to my gridview:

 Protected Sub grdHist_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdHist.RowDataBound

      Dim col1 As String = HttpUtility.HtmlDecode(e.Row.Cells(2).Text)

      e.Row.Cells(2).Text = col1

End Sub
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
2

Booysen's answer works but only for one column. If you run a loop in the RowDataBound event, you can substitute a variable for the [0] and have this work on each column if you want. Here's what I did:

protected void gridCart_RowDataBound(object sender, GridViewRowEventArgs e)
{
    for (int i = 1; i < 4; i++)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string decode = HttpUtility.HtmlDecode(e.Row.Cells[i].Text);
            e.Row.Cells[i].Text = decode;
        }
    }
}

Mine is deliberately started at 1 because of my data, but obviously it will work with whatever you need.

John
  • 21
  • 1
1
protected void gvHead_OnRowDataBound(object sender, GridViewRowEventArgs e) {
  for (int i = 0; i < e.Row.Cells.Count; i++) 
    e.Row.Cells[i].Text = HttpUtility.HtmlDecode(e.Row.Cells[i].Text);
}
Hank Brandenburg
  • 319
  • 3
  • 12
1

You have to bind to the the DataBoundGrid event and change the Rendering for the columns you want to render HTML code.

public event EventHandler DataBoundGrid {
  add { ctlOverviewGridView.DataBound += value; }
  remove { ctlOverviewGridView.DataBound -= value; }
}

ctlOverview.DataBoundGrid += (sender, args) => {
  ((sender as ASPxGridView).Columns["YourColumnName"] as GridViewDataTextColumn).PropertiesTextEdit.EncodeHtml = false;
};
szuuuken
  • 896
  • 10
  • 12
0

@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:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  string decodedText = HttpUtility.UrlDecode(e.Row.Cells[0].Text);
  e.Row.Cells[0].Text = decodedText;
}
Reza Paidar
  • 863
  • 4
  • 21
  • 51