Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping GridView text without a visible space

I have a GridView with several fields, one of which can potentially have a crazy wide value in it like this:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

If that sort of thing is in the field, I want it to wrap.

I can easily insert a character in code every 50 characters or so...but what character? If I use \r\n or a space or the like, then sometimes the setting doesn't wrap (because a different row's 50 chars is wider), and I get something like this:

mmmmmmmmmm
mmmmm
llllllllll llllllllll llll

I don't want those spaces to show up; I just want the line to wrap there if it can, but otherwise to show nothing:

mmmmmmmmmm
mmmmm
llllllllllllllllllllllll

Also, I'd rather leave HtmlEncode on if possible. Is there a way to do this?

like image 866
Ryan Lundy Avatar asked Dec 03 '08 21:12

Ryan Lundy


2 Answers

ItemStyle-Wrap="true" ItemStyle-Width="100" doesn't work for one long word with no space.

Found the solution:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[1].Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
                }
            }

where Cells[1] is the column you want to wrap.

like image 143
Mits Avatar answered Nov 09 '22 07:11

Mits


stick these lines of CSS in a class, and stick that class in your grid items

white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */
white-space: -pre-wrap; /* Opera 4 - 6 */ 
white-space: -o-pre-wrap; /* Opera 7 */ 
white-space: pre-wrap; /* CSS3 */ 
word-wrap: break-word; /* IE 5.5+ */
like image 34
Matt Briggs Avatar answered Nov 09 '22 07:11

Matt Briggs