如题,我想把一些内容的标题显示出来的话,我想达到整齐的效果,要把那些标题的字数限定,请如如何实现呢,谢谢

解决方案 »

  1.   

            /// <summary>
            /// 截取字符串函数
             /// </summary>
            /// <param name="str">所要截取的字符串</param>
            /// <param name="num">截取字符串的长度</param>
            /// <returns></returns>
            static public string GetSubString(string str, int num)
            {
                #region
                return (str.Length > num) ? str.Substring(0, num) + "......" : str;
                #endregion
            }
    这就ok了
      

  2.   

      ///  <summary> 
            /// 截取字符串函数 
             ///  </summary> 
            ///  <param name="str">所要截取的字符串 </param> 
            ///  <param name="num">截取字符串的长度 </param> 
            ///  <returns> </returns> 
    这些是写在哪里的呢,这样写吗
        <summary> 
            GetSubString()
               </summary> 
             <param name="str">dt["家庭住址"].ToString()</param> 
            <param name="num">20</param> 
            <returns> </returns> 
      static public string GetSubString(string str, int num) 
            { 
                #region 
                return (str.Length > num) ? str.Substring(0, num) + "......" : str; 
                #endregion 
            } 
    不好意思,我真的不会,我就是想让家庭住址的字符限定在10个字内,其它用省略号表示,能不能帮我写一个完整的例子,谢谢了
      

  3.   

    很简单的其实,在Grid View的DataBound事件里用Substring(0,10)处理一下就可以了,
    如果你是自己写代码绑定的Grid View就在绑定前把DataSet里的指定列foreach调用Substring(0,10)后,
    再绑定就可以了,我还是觉得第一种简单些。protected void GridView1_DataBound(object sender, EventArgs e)
        {
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                if (GridView1.Rows[i].Cells[2].Text.Length > 10)//Cells[2]是你要限定字符的列数
                {
                    GridView1.Rows[i].Cells[2].Text = GridView1.Rows[i].Cells[2].Text.Substring(0, 10) + "...";
                }
            }
        }
      

  4.   

    在行绑定事件中直接处理:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 假设列Cells[1]是家庭住址
                if (e.Row.Cells[1].Text.Trim().Length > 10)
                {
                    e.Row.Cells[1].Text = e.Row.Cells[1].Text.Trim().Substring(0, 10) + "......";
                }
            }
        }
      

  5.   

    怎么还是不行的,我的就只有一列的,第一列是不是Cell[0]呀,这个0应该是指第一列吧,照5楼那样
       protected void GridView2_DataBound(object sender, EventArgs e)
        {
            for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
            {
                if (GridView2.Rows[i].Cells[0].Text.Length > 10)//Cells[2]是你要限定字符的列数 
                {
                    GridView2.Rows[i].Cells[0].Text = GridView2.Rows[i].Cells[0].Text.Substring(0, 10) + "...";
                }
            }
        } 不行呀六楼这样也不行
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
           if (e.Row.RowType == DataControlRowType.DataRow) 
            { 
              
                if (e.Row.Cells[0].Text.Trim().Length > 10) 
                { 
                    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Trim().Substring(0, 10) + "......"; 
                } 
            } 
        }
      

  6.   

    上面写错了,是GridView2的,我写错了,还是不行
      

  7.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
        {  
           for (int i == 0;i<e.Row.Count -1;i++)  
            {  
     
                    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Trim().Substring(0, 10) + "......";   
            }  
        }
      

  8.   

    还是不行的,我的代码如下:public partial class article : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            string mycon = ConfigurationSettings.AppSettings["mc"];
            SqlConnection con = new SqlConnection(mycon);
            SqlDataAdapter mycom=new SqlDataAdapter("select top 5 * from bjb",con);
            DataSet ds=new DataSet();
            mycom.Fill(ds,"bjb");
            GridView1.DataSource=new DataView(ds.Tables[0]);
            GridView1.DataBind();
            SqlDataAdapter com = new SqlDataAdapter("select top 10 * from student", con);
            DataSet dt = new DataSet();
            com.Fill(dt, "student");
            GridView2.DataSource = new DataView(dt.Tables[0]);
            GridView2.DataBind();
          }
        protected void GridView2_DataBound(object sender, EventArgs e)
        {
            for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
            {
                if (GridView2.Rows[i].Cells[0].Text.Length > 10) 
                {
                    GridView2.Rows[i].Cells[0].Text = GridView2.Rows[i].Cells[0].Text.Substring(0, 10) + "...";
                }
            }
        } 
    可是运行结果如下:
    家庭住址 
    您选择了家庭住址是广东省佛山市顺德 的学生 
    您选择了家庭住址是广东省梅州市丰顺县 的学生 
    您选择了家庭住址是广东省河源市紫金县 的学生 
    您选择了家庭住址是广东省揭阳市惠来县 的学生 
    您选择了家庭住址是广东省珠海市 的学生 
    您选择了家庭住址是广东省梅州市大埔县 的学生 
    您选择了家庭住址是广东省连州市 的学生 
    您选择了家庭住址是广东省广州市 的学生 
    您选择了家庭住址是广东省广州市 的学生 
    您选择了家庭住址是广东省汕头市金平区 的学生 
      

  9.   

    用这个吧:http://download.csdn.net/source/303583