gridview Chinese text cannot be wrapped (auto wrap/Normal line Wrap) _ Practical Tips

Source: Internet
Author: User
When I was working on a project recently, I had this problem: I used the GridView to display my schedule, and each cell included the course name, the class location, the teacher's name, and then I wanted to show them the branches, and the effect was as follows:

However, the GridView is too tenacious, no matter when I stitching strings with "\ r \ n" or "<br/>", the results are useless, do not show branches. After nearly half a day of time to query information, finally resolved the problem, the following share with you.

The line-wrapping problem in the GridView can be divided into two categories: one is to wrap the line, the other is normal line-wrapping.

First class: Wrapping automatically.

The GridView default is wrapping automatically, which means the GridView wraps automatically when the displayed string is longer.

For example, we want to show: "When the string is longer, the line will be automatically wrapped," the effect is as follows:

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

Copy Code code as follows:

Normal line Wrapping
GRIDVIEW1.ATTRIBUTES.ADD ("Style", "word-break:keep-all;word-wrap:normal");

Add the following effect chart as follows:

here is the second category: Normal line wrapping .

The key to normal line break is the choice of line breaks, some people say with the escape character "\ r \ n", others say "<br/>". So which one is it? Let's look at the results of these two shows:

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

Show the effect as shown below (and no plus):

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

The display effect chart is as follows:

As can be seen from the above, the single addition of line breaks can not solve the problem. The correct solution is as follows:

1, should use "<br/>"

① If you bind the field to set the template column, then set the corresponding BoundField parameter htmlencode= "false".

② If you are generating fields automatically: Add GridView1_RowDataBound events

Copy Code code as follows:

So that the contents of the GridView can be wrapped.
<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 of the HTML code in this column will be exported in HTML format, and if only one of the columns of data needs to be converted, small modifications are required here.

Look at the results after adding:

Display string: "When the string" + "<br/>" + "longer" + "<br/>" + "Here's the line."

Display effect:

So far, the problem is solved perfectly.

Related Article

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: 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.