Search code examples
c#asp.netstringsql-server-2008line-breaks

LineBreak not working in Binding of GridView


In an ASP.NET application, I have multiline string values as listed below.

Code:

string remarks="Line 1 \n";
remarks+=remarks+"\n Line 2 \n";

Console.Writeln(remarks);

Output:

Line 1

Line 2

Now I want the same output of the string values, when I insert them into a table on a SQL Server, retrieve them back an then present them in a GridView by bindings.

But after the retrieval from the DB, I'm getting the whole string in one line only like this (the line breaks are not working):

Line 1Line 2

I tried Environement.NewLine and StringBuilder but I always get the output from the server as one-liner.

What am I doing wrong?


Solution

  • Replace "\n" with "
    " on row bound event

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace("\\n", "<br/>");
                }
            }