I need to dynamically modify the contents of a column in a GridView1 before it is displayed. Basically, I need to convert every 'Environment.NewLine' in a field to a
so it displays as a new line on an ASP.NET page.
How do I do this?
I need to dynamically modify the contents of a column in a GridView1 before it is displayed. Basically, I need to convert every 'Environment.NewLine' in a field to a
so it displays as a new line on an ASP.NET page.
How do I do this?
You can use the RowCreated event to alter rows as they have been created and then edit specific columns. I think to get the columns you use
void ProductsGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
e.Row.Cells[1] += "<br />"
}
I assume you're using a data source to bind this grid. If so, I'd suggest the RowDataBound event. I doubt you'd have any newlines in your header row, but there isn't much sense in checking or appending HTML to them.
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Text.Replace(Environment.NewLine, "<br />");
}
}
}