GridView文本自动换行

首先是GridView 中的文本不自动换行,隐藏超出宽度部分,可以使用以下函数:


return (str.Length > num) ? str.Substring(0, num) + "..." : str;

int num 是你要截取的字符串的长度,然后把这个函数写在行绑定数据的事件中即可。

要是要使GridView里的内容自动换行的话,使用以下办法

在页面加入属性:style =" word-break :break-all ; word-wrap:break-word " (table或div 等的属性里)

或设置
protected void GridView1_DataRowBound(object o, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//设置要换行的模板列
e.Row.Cells[0].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
e.Row.Cells[1].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
}
}
或者
如何实现:GridView 控件中显示的文本不自动换行,隐藏超出宽度部分
    <asp:TemplateField>
                                <ItemTemplate>
                                     <asp:LinkButton ID="linkbNewsTitle" runat="server" Text='<%# Bind("ch_Remark") %>' ToolTip='<%# Bind("ch_Remark") %>'>
                                     </asp:LinkButton>
                                </ItemTemplate>
                                <ItemStyle Width="300" />
                            </asp:TemplateField>
protected void gvNewsManage_RowDataBound(object sender, GridViewRowEventArgs e)
    {
if ((LinkButton)e.Row.FindControl("linkbNewsTitle") != null)
        {
            LinkButton linkbTitle = (LinkButton)e.Row.FindControl("linkbNewsTitle");
            if (linkbTitle.Text.Length > 10)
            {
                linkbTitle.Text = linkbTitle.Text.Substring(0, 9) + "";
           
            }
            Label lblinknewID = (Label)e.Row.FindControl("ltgvnid");
           // linkbTitle.PostBackUrl = "EditNewsContent.aspx?ID=" + lblinknewID.Text;
       
        }
}

//==========================================
protected void gvNewsManage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string strValue = e.Row.Cells[18].Text.ToString();
            if (strValue != null)
            {
                if (strValue.Length > 9)
                {
                    e.Row.Cells[18].Text = strValue.Substring(0, 9) + "";
                }
            }
        }

 

或者

首先确定你的GRIDVIEW 已经绑定数据了

第一步,在GRIDVIEW 上右击勾选"显示智能标记"

第二步,点击"编辑列"

第三步,在左边"选定的字段"中选中你不想行换行的字段

第四步,在右边的"BoundFiled属性"里展开
"样式"--"ItemStyle"--"Wrap"改成false

标题(HeadStyle) 的情况类似....

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值