gridview绑定的标题列一行大概放20个汉字,如果多了自动换行,页面就不美观了怎么让标题需要换行的末尾显示省略号,鼠标移到那显示全部内容

解决方案 »

  1.   

    在RowDataBound事件中进行判断 ,
    鼠标移到那显示全部内容——————设置该单元格的title属性为标题内容;
      

  2.   

    用截取法,title属性放置鼠标显示全部内容
      

  3.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string sTmp;
                string pic;
                if (((DataRowView)e.Item.DataItem)["FileTitle"] != DBNull.Value)
                {
                    sTmp = Convert.ToString(((DataRowView)e.Item.DataItem)["FileTitle"]);
                    pic = Convert.ToString(((DataRowView)e.Item.DataItem)["IsImage"]);
                   
                    if (pic == "1")
                    {
                        string stmp2 = "";
                        if (sTmp.Length <= 19)
                            stmp2 = sTmp + "(图)";
                        if (sTmp.Length > 19)
                            stmp2 = sTmp.Substring(0, 18) + "…(图)";
                        sTmp = stmp2;
                    }
                    else
                    {
                        if (sTmp.Length > 21)
                            sTmp = sTmp.Substring(0, 20) + "…";
                    }
                    ((HyperLink)e.Item.FindControl("hylzcwj")).Text = "˙" + sTmp;            }
            }
        }
      

  4.   


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
         for (int i = 0; i < e.Row.Cells.Count; i++)
                        {
                            if ((e.Row.Cells[i].Text != "") && (i == 5) && (e.Row.Cells[i].Text != null))
                            {
                                str = e.Row.Cells[i].Text;
                                e.Row.Cells[i].Text = cutStr(e.Row.Cells[i].Text, 20);
                                e.Row.Cells[i].Text = "<a  style='cursor:hand;' title='" + str + "'>" + e.Row.Cells[i].Text + "</a>";
                            }
       }
    }
    //devidefunction.cutStr是截取字符穿方法20代表只显示20个字符,这样就可以做到了
    //*******************************文字截断加……函数*******************************
            public static string cutStr(string str, int num)
            {
                if ((str.Length) > num)
                {
                    str = "&nbsp;&nbsp;" + str.Substring(0, num) + "...";
                    return str;
                }
                else
                {
                    return str;
                }
            }
      

  5.   

    直接写个截取的方法
    截就行了.6楼的这个方法即可. public static string cutStr(string str, int num)
            {
                if ((str.Length) > num)
                {
                    str = "&nbsp;&nbsp;" + str.Substring(0, num) + "...";
                    return str;
                }
                else
                {
                    return str;
                }
            }
      

  6.   

    关键那我用的超链接(<a></a>)没有title属性,我怎么获取那个内容啊
      

  7.   


    //解决方法:数据绑定后过滤每一行即可
    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;
        }
      

  8.   

    下面这段代码完全符合楼主要求       <table id="Table1" cellspacing="1" cellpadding="1" width="300" border="1">
            <tr>
                <td width="100">
                    <div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100px;" title="测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试">
                        测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试</div>
                </td>
            </tr>
        </table>显示为: 测试测试测...
      

  9.   

    我用6楼的这个方法             if ((e.Row.Cells[2].Text != "") || (e.Row.Cells[2].Text != null))
                {
                    string str = e.Row.Cells[2].Text.Trim();   str="" 这是为什么?
                    e.Row.Cells[2].Text = cutStr(str, 40);
                }
      

  10.   

    对,可以用截取字符串处理,如left函数,只是取标题的前20个汉字,后续的显示为....,但是要说明的是text,ntext字段的标题是不能这么处理的。
      

  11.   

    http://www.dezai.cn/article_show.asp?ArticleID=15602
    去看看
      

  12.   

    另外一种思路:
    在模板列中将要显示的字段改成TextBox(只读,背景颜色透明,边框去掉),鼠标扫过可显示全部.
    或再加toolTip属性!!
      

  13.   

    //str 是你的字符串,num截取长度,flg是否带...
    public static string CutString(string str, int num, bool flg)
        {
            #region
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(str);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }            try
                {
                    tempString += str.Substring(i, 1);
                }
                catch
                {
                    break;
                }            if (tempLen > num)
                    break;
            }
            //如果截过则加上半个省略号
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(str);
            if (mybyte.Length > num)
                if (flg)
                {
                    tempString += "...";
                }
            return tempString;
            #endregion
        }
      

  14.   

    要吗GridView1_RowDataBound过滤长度
    要吗用个过滤长度的函数然后使用在模板列中
    要吗你设置一下那个属性让它自动换行