我有一张新闻表(ID,Title,Content),我想实现在DataGrid中显示Title, 有时候Title的字符比较长,如果都显示会影响美观,所以我想只显示10个字符,其余的用“...”表示!还望各位教教小弟!谢谢了!

解决方案 »

  1.   

    1.用sql语句 select id,(if len(title)>10 then left(title,10)+'...' else title) as title from table name
    2.在itemDataBound中写
       string temp=e.Item.Cell[2].Text.Trim()
       if(temp.Length>10)
       {
            e.Item.Cell[2].Text=temp.Substring(0,10)+"...";
        }
      

  2.   

    datagrid在绑定的地方取一下就行了,或者在代码中写一个函数,绑定就可以了
      

  3.   

    //截取操作
    public string MyLeftFunction(string strInput,int intLen)
    {
    strInput=strInput.Trim();
    byte[] myByte = System.Text.Encoding.Default.GetBytes(strInput);   
    if(myByte.Length>intLen)
    {

    string resultStr="";
    for(int i=0;i<strInput.Length;i++)
    {
    byte[] tempByte=System.Text.Encoding.Default.GetBytes(resultStr);
    if(tempByte.Length<intLen)
    {
    resultStr+=strInput.Substring(i,1);
    }
    else
    {
    break;
    }     
    }
    return resultStr+"...";
    }
    else
    {
    return strInput;
    }
    }
    前台
    <%# MyLeftFunction(DataBinder.Eval(Container, "DataItem.Title").ToString(),20) %>
      

  4.   

    public static string CutString(string str, int length)
            {
                int i = 0, j = 0;
                foreach (char chr in str)
                {
                    i += 2;
                    if (i > length)
                    {
                        str = str.Substring(0, j - 1) + "...";
                        break;
                    }
                    j++;
                }
                return str;
            }
      

  5.   

    LS几为仁兄的代码应该都可以的啊?LZ有什么错误啊?
      

  6.   

    mid(eval("title"),1,20)
    截取20个字(这是我经常用的一招)
      

  7.   

    mid是vb.net中的一个函数,不知道c#中是否有