关于GridView中超长字符串显示时,我们想如此处理:截取其中前几个字符串,然后在这几个字符串后面加"...",而且通过某种方式能查看超长字符串的内容?

解决方案 »

  1.   

    如果你不在意页面还是那么大的话,你可以在显示的地方放一个div,包含要显示的全部内容,然后当鼠标放到你显示的那几个字符串上的时候,就显示出来。
    或者放到你cell的alt里面,鼠标放上去停顿下就会出现,不过估计浏览者不会有那个耐心。
    还有就是别人点击后在另外的窗口显示内容。
    再有就是利用ajax技术,别人点击你显示的内容,你用ajax到后台获取全部内容用div包括显示出来。
      

  2.   


    //解决方法:数据绑定后过滤每一行即可
    for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                DataRowView mydrv;
                string gIntro;
                if (GridView1.PageIndex == 0)
                {
                    mydrv = myds.Tables["表名"].DefaultView[i];//表名
                    gIntro = Convert.ToString(mydrv["字段"]);//所要处理的字段
                    GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
                }
                else
                {
                    mydrv = myds.Tables["表名"].DefaultView[i + (5 * GridView1.PageIndex)];
                    gIntro = Convert.ToString(mydrv["字段"]);
                    GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
                }
            } //调用的方法:    public string SubStr(string sString, int nLeng)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            string sNewStr = sString.Substring(0, nLeng);
            sNewStr = sNewStr + "...";
            return sNewStr;
        }
    //希望能给你帮助
      

  3.   

    在html源 中,找到对应的字段绑定项,设置为模版列:<ItemTemplate>
    <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.content").ToString().Length>10?DataBinder.Eval(Container, "DataItem.content").SubString(0,10)+"...":DataBinder.Eval(Container, "DataItem.content").ToString() %>' ToolTip='<%# DataBinder.Eval(Container, "DataItem.content").ToString()%>'>
    </asp:Label>
    </ItemTemplate>
      

  4.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //控制字符显示长度,若大于18个字符,显示前18个,后加"..."
            for (int i = 0; i <e.Row.Cells.Count; i++)
            {
                if (e.Row.Cells[i].Text.Length > 18)
                {
                    e.Row.Cells[i].Text = e.Row.Cells[i].Text.Substring(0, 18) + "...";
                }
            }    }
      

  5.   

    在后台(cs文件)中写一个方法:
            /// <summary>
            /// 裁取指定长度的字符串,被裁取的字符串以"..."替代。
            /// </summary>
            /// <param name="mString">用于要裁取的字符串</param>
            /// <param name="mLength">长度</param>
            /// <returns>返回String</returns>
            public string SubString(string mString, int mLength)
            {
                if (mString.Length > mLength)
                    return mString.Substring(0, mLength-2) + "...";
                else
                    return mString;
            }
    然后在GridView中用模板的方法绑定
    <asp:GridView ID="GridView1" runat="server">
       <Columns>
            <asp:TemplateField HeaderText="超长字符">
                 <ItemTemplate>
                     <%# SubString(Eval("超长字符字段名").ToString(), 18) %>
                 </ItemTemplate>
            </asp:TemplateField>
       </Columns>
    </asp:GridView>
      

  6.   

                                <asp:TemplateField HeaderText="备注"  > 
                                <ItemTemplate>
                                <asp:Label ID="Lable1" runat="server" Text='<%#Eval("家庭住址").ToString().Length > 10 ? Eval("家庭住址").ToString().Substring(0, 10) + "..." : Eval("家庭住址")%> ' ToolTip='<%# Eval("家庭住址").ToString()%>'></asp:Label>                                    
                                </ItemTemplate> 
                                </asp:TemplateField> 
      

  7.   

       <asp:TemplateField HeaderText="备注"  > 
                                <ItemTemplate> 
                                <asp:Label ID="Lable1" runat="server" Text=' <%#Eval("家庭住址").ToString().Length > 10 ? Eval("家庭住址").ToString().Substring(0, 10) + "..." : Eval("家庭住址")%> ' ToolTip=' <%# Eval("家庭住址").ToString()%>'> </asp:Label>                                    
                                </ItemTemplate> 
                                </asp:TemplateField> 
    最近做的一个项目就是这样用的,不错(⊙o⊙)哦!
      

  8.   

    我提供一个SQL语句从数据库中选择数据时截取字符串的方法:select ID,
    case 
    when len(pDescription)>3
    then left(pDescription,3)+'...'
    else pDescription
    end
    as Description
    from Products
      

  9.   

    <asp:TemplateField HeaderText="备注"  > 
                                <ItemTemplate> 
                                <asp:Label ID="Lable1" runat="server" Text=' <%#Eval("家庭住址").ToString().Length > 10 ? Eval("家庭住址").ToString().Substring(0, 10) + "..." : Eval("家庭住址")%> ' ToolTip=' <%# Eval("家庭住址").ToString()%>'> </asp:Label>                                    
                                </ItemTemplate> 
                                </asp:TemplateField> 
    学习 
      

  10.   

    1,将要substring的列设为模板列,在gridview的ondatabound事件里处理,显示可以用超链接或层
      

  11.   

    在页面加载的时候加个
    GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
    就可以了.试试吧!
      

  12.   

    截断的字符串用ToolTip显示出来public void OmitGridInfo(System.Web.UI.WebControls.GridViewRowEventArgs e, int cellIndex, int maxStrLength)
        {        string CellText = e.Row.Cells[cellIndex].Text.Trim();        if (CellText.Length > maxStrLength)
            {            e.Row.Cells[cellIndex].Text = CellText.Substring(0, maxStrLength - 1) + "<strong style='Color:blue'> ...</strong>";            e.Row.Cells[cellIndex].ToolTip = CellText;
            }    }
      

  13.   

    顶~~~ getlength() 这个方法可以吗?
      

  14.   

    <asp:GridView ID="GridView1" runat="server"> 
      <Columns> 
            <asp:TemplateField HeaderText="超长字符"> 
                <ItemTemplate> 
                    <%# SubString(Eval("超长字符字段名").ToString(), 18) %> 
                </ItemTemplate> 
            </asp:TemplateField> 
      </Columns> 
    </asp:GridView> 
    可行
      

  15.   

    楼主看看我这段,我以前用过的。
    <%#(DataBinder.Eval(Container.DataItem, "new_content ")).ToString().Length < 115 ? (DataBinder.Eval(Container.DataItem, "new_content ")) : ((DataBinder.Eval(Container.DataItem, "new_content ")).ToString().Substring(0, 115)+"......")%> 
      

  16.   

    public string SubStr(string InputStr,int nleng)
    {
    if(InputStr.length<=nleng)
    {
    return InputStr;
    }
    else
    {
    string NewStr=InputStr.SubString(0,nleng);
    NewStr=NewStr+"......";
    return NewStr;
    }
    }
    <%#SubStr(DataBinder.Eval(Container.DataItem,"Content").ToString()),5%>
    二种方法:
    GridView_RowDataBound()
    {
    if(e.Row.RowType==DataControlRowType.DataRow)
    {
    if(e.Row.Cells[2].Text.Length>=5)
    {
    e.Row.Cells[2].Text=e.Row.Cells[2].Text.SubString(0,5)+"..........";
    }
    }
    }
      

  17.   

        后台代码:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strName = e.Row.Cells[0].Text;
                if (strName.Length > 5)
                    e.Row.Cells[0].Text = strName.Substring(0, 5) + "....";
                e.Row.Cells[0].ToolTip = strName;
            } 
        }