Search This Blog

Thursday, September 29, 2011

How to use GridViewDataRow with html contents.

If datasource(datatable) has html contents and bind this datasource to the gridview.
The GridView will show the data as it is in datatable.It means it will show the html content as it is.
For Example:
Your DataTable has html content "<Strong>Testing</Strong>" but gridview display same "<Strong>Testing</Strong>" instead of "Testing".

Solution:

Add the following code into gridView's Rowdatabound event.

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
foreach (TableCell cell in e.Row.Cells)
{
cell.Text = Server.HtmlDecode(cell.Text);
}
}

No comments:

Post a Comment