Murugan
Murugan

Reputation: 33

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?

Upvotes: 0

Views: 2273

Answers (2)

Chandrashekar Jupalli
Chandrashekar Jupalli

Reputation: 347

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/>");
            }
        }

Upvotes: 1

Neal
Neal

Reputation: 811

SQL will maintain inserted linebreaks.

Linebreaks won't show up on the likes of a DataGridView object by default. Try:

dataGridView1.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;

Upvotes: 0

Related Questions