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?
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.
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+ */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With