控制gridview显示字段中显示的字数

来源:互联网 发布:玉田二中网络u盘 编辑:程序博客网 时间:2024/04/26 07:50

控制gridview显示字段中显示的字数

方法一 在Sql Server里控制:
使用SQL中的函数Left 和Right ,Sql 语句 :select left(你的字段名,你要显示的字数)from Table

方法二 在ASP.NET页面中设计代码实现:
数据直接绑定时控制

1<%# DataBinder.Eval(Container.DataItem,"title").ToString().Length>12?DataBinder.Eval(Container.DataItem,"title").ToString().Substring(0,20)+"":DataBinder.Eval(Container.DataItem,"title").ToString()%>
2
或者绑定方法

 public   static   string   InterceptStr(string   str,   int   lenght) 
  { 
       if   (str.Length   >   lenght) 
         { 
                return   str.Substring(0,   lenght   -   3)   +   " "; 
          } 
      else 
         { 
                return   str; 

           } 

  }