6

I want to know how to put a line break in a cell of a grid view. Right now, I put

1
1
1

However, this renders as

1 1 1

How do I fix the line breaks so that each digit is displayed on its own row?

Vivian River
  • 31,198
  • 62
  • 198
  • 313
ritika
  • 61
  • 1
  • 1
  • 2
  • Possible duplicate of [How do I break the a BoundField's HeaderText](https://stackoverflow.com/questions/310121/how-do-i-break-the-a-boundfields-headertext) – Michael Freidgeim Oct 01 '19 at 04:19

4 Answers4

11

Add HtmlEncode="False" to asp:BoundField and in the text, should have < br/> for line breaks as:

<asp:BoundField DataField="Address" HeaderText="Address" HtmlEncode="False" />
Dil
  • 111
  • 1
  • 4
4

You'll need to use the <br/> tag to put a line break in your html.

You can use String.Replace(new char[] { '\n' }, "<br>") to get the values with line-breaks replaced with <br/> in C#.

Vivian River
  • 31,198
  • 62
  • 198
  • 313
0

In the GridView1_RowDataBound(object sender, GridViewRowEventArgs e) you can put:

e.Row.Cells[2].Text = e.Row.Cells[2].Text.Replace("\n", "<br/>");

For whatever column you're updating. My example is for the third column (e.Row.Cells[2]).

Crazy Cat
  • 1,332
  • 15
  • 19
-1

in gridview's rowdatabound event, you can check the cell's value's count of digit, and if that value is bigger than 1, you can add html tag <br /> between them.

but this won't make the digits show in another row, it will still be the same row, just larger (cause it will be for example 3 lines if the value is 111)

  • i have multiple names like shakti singh shweeta agarwal then in one cell shakti singh – ritika Nov 10 '10 at 07:21
  • i have multiple names in a row i want each name should be displayed in separate row eg in my data sorce i have multiple names in a column's row i want when data bind to gridview each name must be displayed in separate row. – ritika Nov 10 '10 at 07:33
  • i would suggest to you 1)if you can, change your "select" query for your datasource 2) create a table at code behind, add its columns, fill its cells and then give it as a datasource to your control 3) at rowdatabound event, get the cell's value's count of digit. if its bigger than 1, create a new row for every single digit and add them to your control. – serhat_pehlivanoglu Nov 10 '10 at 08:53