December 25, 2010

Replacing \n line breaks with HTML line breaks in Gridview

I was Inputting values from textArea and showing it in a gridview .
I realized whenever I m Pressing Enter I m not getting the line break in GridView.
because while Saving values in DB from TextArea ,was saving Line Break in newline Char (\n)
which is Non-HTMl Char and won't be read in GridView.
So Below is the solution I used for replacing it with HTML Break (<br/>).

protected void gridHistory_RowDataBound(object sender, GridViewRowEventArgs e)
{
 GridViewRow row = e.Row;
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  row.Cells[0].Text = row.Cells[0].Text.Replace("\n", "<br />");
 }
}

No comments:

Post a Comment