Text Content in ASP. NET gridview cannot wrap

Source: Internet
Author: User

Recently, when I was working on a project, I encountered the following problem: I used a gridview to display the course schedule. The content of each cell includes the course name, course location, and instructor name. Then I wanted them to be displayed in a branch, the effect is as follows:

However, the gridview is so tenacious that no matter whether I use "\ r \ n" or "<br/>" to concatenate a string, the results will not help, and no branch will be displayed. After nearly half a day of data query, the problem was finally solved. Let's share it with you.


The line feed problem in the gridview can be divided into two categories: one is automatic line feed and the other is normal line feed.


First, let's talk about the first class: automatic line feed.

The default value of the gridview is automatic line feed. That is to say, when the displayed string is long, the gridview will automatically line feed.

For example, we want to display: "When the string is long, it will automatically wrap the line." The effect is as follows:

Of course, if we don't want it to wrap automatically, just add the following code in the background of the page:

// Normal line feed gridview1.attributes. Add ("style", "word-break: Keep-all; Word-wrap: normal ");

After adding:


Next we will talk about the second type: normal line feed.

The key to a normal line feed is the selection of line breaks. Some people say that the Escape Character "\ r \ n" is used, and others say "<br/>" is used ". Which one is used? Let's take a look at the two display effects:

1. display the string: "When the string" + "\ r \ n" + "is long, \ r \ n will automatically wrap the line"

Shown as follows (same as ):

2. display the string: "When the string is relatively long" + "<br/>" + "here is the line feed"

Shown as follows:

It can be seen from the above that adding a line break alone cannot solve the problem. The correct solution is as follows:

1. You should use "<br/>"

2. ① if you bind a field to the set template column, set the corresponding boundfield parameter htmlencode = "false.

② If fields are automatically generated: add the gridview1_rowdatabound event.

/// <Summary> /// enables the content in the gridview to wrap. /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> protected void gridview1_rowdatabound1 (Object sender, gridviewroweventargs e) {If (E. row. rowtype = datacontrolrowtype. datarow) {tablecellcollection cells = E. row. cells; foreach (tablecell cell in cells) {Cell. TEXT = server. htmldecode (cell. text); // Note: All HTML code in all columns is output in HTML format. Data in one column needs to be converted. A small modification is required here. }}}

Check the display effect after adding:

Display string: "When the string" + "<br/>" + "is relatively long" + "<br/>" + "here is the line feed"

Display Effect:

So far, the problem has been solved perfectly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.