ASP.NET GridView中文本内容无法换行

第一类:自动换行

GridView默认是自动换行,就是说当显示的字符串比较长的时候,GridView会自动换行。

如果我们不想让它自动换行,在页面后台添加如下代码即可:

//正常换行 
GridView1.Attributes.Add("style","word-break:keep-all;word-wrap:normal");

第二类:正常换行

1、应该使用 "<br/>"

2、
①如果你绑定字段为设置模版列,那么把对应的BoundField设置参数HtmlEncode= "false" 即可。

②如果为自动生成字段:请添加GridView1_RowDataBound事件

/// <summary> 
/// 使得GridView中的内容可以换行 
/// </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); 
        } 
    } 
}

posted on 2016-12-18 23:21  lmx22  阅读(1783)  评论(0编辑  收藏  举报

导航