views:

1713

answers:

3

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?

A: 

You can do a few things here.

One option is change the column in question to a TemplateColumn, and place a div in it, with the ability to generate scrollbars. I'd really suggest against this.

If you use a CRLF (\r\n) then you'd need to wrap it into PRE tags, and you'd want to change the font fact to a monospace font (such as Courier) so that 50 characters would ALWAYS be the same length.

If you want an HTML solution, then it's a matter of inserting a BR tag into the text at the appropriate place.

Now, you'd also want to ensure that you're checking the length of the string (making sure that it's greater than your magic number length), and that it doesn't contain a space (as a space will allow for wrapping), prior to inserting either the CRLF or the BR.

Stephen Wrighton
+3  A: 

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+ */
Matt Briggs
I'd go with a CSS only solution like this one +1. Adding crap to the output is annoying and should be avoided. Handle the UI stuff in the markup, I say.
Zachary Yates
A: 

"wbr" Tag is used for that.That should be placed in angle bracket("<>").I'm representing it as wbr in the code given below as it is an html tag.

Refer this site http://blog.renjucool.com/gridview

Renju