Problem:
You want to display HTML formatted content in a GridView, but HTML tags are escaped and visible in the browser. As far as I know, the GridView BoundField is the only field that does a .HtmlEncode.
Solution:
.HtmlDecodethe text in each cell onRowDataBound
Something like:Sub gv_drb(sender As Object, e As GridViewRowEventArgs) Handles gv.RowDataBound For Each cell As TableCell In e.Row.Cells cell.Text = Server.HtmlDecode(cell.Text) Next End SubThis is not be the most efficient solution since each cell will be first encoded automatically and then decoded by you, but that overhead may be negligible.
Note:
- I learned here that: “Setting the Text property will clear any other controls contained in the TableCell.”
So, with the above method, you would loose, for example, links and buttons in autogenerated update, select, etc. columns. For autogenerated columns it is sufficient to check if there is Text, since their Text property is empty:
If cell.Text <> "" ... - You might want to limit the type of row to decode, i.e. not decoding header cells:
If Not e.Row.RowType = DataControlRowType.Header...
- I learned here that: “Setting the Text property will clear any other controls contained in the TableCell.”
- Write a custom GridView control
To prevent HTML encoding in the first place, overrideInitializeCelland setallowHtmlEncode=false. I have not tried that, more on the idea can be found at 4 Guys from Rolla. - Use a DataGrid
As mentioned before, htmlencoding is a GridView specific feature. If it is feasible, you could switch to a different control, like a DataGrid.
Other solutions I haven’t tried:

10 comments
Comments feed for this article
June 24, 2008 at 13:35
Phil
Sweet Juice Guy… this was exactly what I needed!!
February 26, 2009 at 10:12
Benjamin Pirih
Nice solved my issue.. Thanks..
C# version
protected void bodyView_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach ( TableCell cell in e.Row.Cells)
{
cell.Text = Server.HtmlDecode(cell.Text);
}
}
March 21, 2011 at 13:17
KalC
Thanks
June 2, 2009 at 14:01
Chris Conley
Just what I was looking for.
I appreciate it.
In my use of it, I already knew the cell I wanted to decode, so, I simply set the code to HTMLDecode only the appropriate cell column, written in C#, below:
e.Row.Cells[3].Text = Server.HtmlDecode(e.Row.Cells[3].Text);
February 27, 2010 at 12:33
babak
you can use this code to prevent HTML encoding in the select button that contain an image only
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
For Each cell As TableCell In e.Row.Cells
If Not cell.Text.Trim().ToString() Is String.Empty Then
cell.Text = Server.HtmlDecode(cell.Text)
End If
Next
End Sub
———————————–
http://www.DotNetSource.com
بابک زواری
babak zawari
بابك زواري
May 18, 2010 at 1:07
Ayaz Maqsood Butt
well friends i have solve this in a real simple manner just add two tags
HtmlEncode=”False” HtmlEncodeFormatString=”False”
in your html in databound column no need to parse whole grid as it consumes a lot of resources making page response slow
my initial field was
and it have top display text html encoded like ayaz para so i add following tags and get the required decoded result by following statement
please let me know if this doesn’t solve your problem i will help u out
Regards
Ayaz Maqsood
ERC Global
Pakistan,Karachi
May 19, 2010 at 18:41
metalinq
sweet!!
June 15, 2010 at 23:38
Scott Hamilton
template field will also render the HTML code properly. You can convert any column to a template by selecting “convert this field int a template field” via the edit columns from smart tag
February 14, 2011 at 14:30
Brandon
Works great…. Thanks guys!!
Sub gv_drb(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles grdCaseNotes.RowDataBound
For Each cell As TableCell In e.Row.Cells
e.Row.Cells(0).Text = Server.HtmlDecode(e.Row.Cells(0).Text)
Next
End Sub
April 20, 2011 at 9:21
Mike P.
I’m very late to the game here, but as this page came up as the first result when I was researching this problem, I thought I’d share my solution as well.
My situation was that we were DataBinding a GridView with AutoGenerateColumns=true, which resulted in the HTML-escaping BoundFields. Since .NET 3.5, GridView has supported a ColumnsGenerator property that allows you to control how the columns are created. With that in mind, here’s my IAutoFieldGenerator class:
///
/// Generates BoundFields for an IDataBoundControl that preserve any HTML within their contents.
///
public class HtmlFieldGenerator : IAutoFieldGenerator
{
ICollection IAutoFieldGenerator.GenerateFields(Control control)
{
List result = new List();
IDataBoundControl gv = control as IDataBoundControl;
if (gv != null && gv.DataSource != null)
{
DataTable dt = gv.DataSource as DataTable;
if (dt == null && gv.DataSource is DataSet && !String.IsNullOrEmpty(gv.DataMember) && ((DataSet)gv.DataSource).Tables.Contains(gv.DataMember))
{
dt = ((DataSet)gv.DataSource).Tables[gv.DataMember];
}
if (dt != null)
{
foreach (DataColumn dc in dt.Columns)
{
result.Add(new BoundField()
{
HeaderText = dc.ColumnName,
DataField = dc.ColumnName,
HtmlEncode = false
});
}
}
}
return result;
}
}
Note the HtmlEncode = false line. Now, for your GridView, you can just say:
GridView gv = new GridView() {
AutoGenerateColumns = true,
// … whatever else you need to set …
ColumnsGenerator = new HtmlFieldGenerator()
};
And your HTML won’t be escaped any longer.