gridview 截取字符串

本文介绍如何在GridView中优雅地处理过长的字符串,通过设置样式限制显示长度,并使用省略号提示用户。同时,提供了ASP.NET中具体的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Gridview中,如果你的某一列字符串的长度过长,不做处理的话.那么将显示的奇丑无比,

可以采取设置样式,将其显示为定长,可以在点击查看的时候,在另一个页面对其进行显示

 

首先在前台设置样式

 

 

[c-sharp]  view plain copy
 
  1. <style  type="text/css">  
  2.  .listover150  
  3. {  
  4. width:150px;  
  5. text-align:left;  
  6. overflow:hidden;  
  7. text-overflow:ellipsis;//超长设置省略号  
  8. white-space:nowrap;  
  9. }  
  10. </style>  

 

 

然后在后台GridView中的RowDataBind中进行设置

,附带几句可以改变鼠标移动的样式设置

 

[c-sharp]  view plain copy
 
  1. //列表加载处理  
  2.    protected void gv_showReport_RowDataBound(object sender, GridViewRowEventArgs e)  
  3.    {  
  4.   
  5.        if (e.Row.RowType == DataControlRowType.DataRow)  
  6.        {  
  7.   
  8.            //当鼠标移开时还原背景色  
  9.            e.Row.Attributes.Add("onmouseout""this.style.backgroundColor=c");  
  10.            e.Row.Attributes.Add("onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#F4FBFF'");  
  11.            e.Row.Attributes.Add("onclick""this.style.backgroundColor='#e2eaf1'");  
  12.        }  
  13.        if (e.Row.RowType == DataControlRowType.Header)  
  14.        {  
  15.            e.Row.Attributes.Add("style""background-image:url('../images/grid3-hrow.gif')");  
  16.        }  
  17.        if (e.Row.RowType == DataControlRowType.DataRow)  
  18.        {  
  19.            //设置申请原因字符串显示长度  
  20.            string strDISC = e.Row.Cells[4].Text.Trim();  
  21.            e.Row.Cells[4].Text = "<div class=/"listover150/">" + strDISC + "</div>";  
  22.            e.Row.Cells[4].ToolTip = strDISC;//鼠标放上去显示所有  
  23.   
  24.            //设置审批备注字符串截取长度  
  25.            string str = e.Row.Cells[7].Text.Trim();  
  26.            e.Row.Cells[7].Text = "<div class=/"listover150/">" + str + "</div>";  
  27.            e.Row.Cells[7].ToolTip = str;  
  28.   
  29.   
  30.               
  31.        }  
  32.    }  

 

转载于:https://www.cnblogs.com/withoutaword/p/3149309.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值