由于其条数据的长度太长,使得用gridview绑定时,改变了gridview的格局,相当难看,我现在想设置一列的宽度,而且想使超过宽度的字用省略号代替,不知道怎么设置,请各位指教。我用过itemstyle设置width与heigh,但是没有效果,字还是全部显示出来。

解决方案 »

  1.   

    处理rowDataBound事件。if(e.row.rowState==dataControlRowState.alternate||e.row.rowState==dataControlRowState.normal){...}
      

  2.   

     <asp:TemplateField HeaderText="标题">
        <ItemTemplate>
         <a href="ShowRules.aspx?ID=<%#Eval("ID")%>&Page=<%=pageindex%>">
               <%#CutString(Eval("Title").ToString(), 40)%>
         </a>
        </ItemTemplate>
      <ItemStyle HorizontalAlign="left" Width="50%" />
     </asp:TemplateField> public string CutString(string str, int length)
        {
            string delsqace = str.Trim();
            int i = 0, j = 0;
            foreach (char chr in delsqace)
            {
                if ((int)chr > 127)
                {
                    i += 2;
                }
                else
                {
                    i++;
                }
                if (i > length)
                {
                    delsqace = delsqace.Substring(0, j) + "....";
                    break;
                }
                j++;
            }
            return delsqace;    }
      

  3.   

     <asp:TemplateField>
     <HeaderTemplate>留言内容 </HeaderTemplate>
     <ItemTemplate>
    <asp:HiddenField Value='<%#Eval("content") %>' ID="HFcontent" runat="server" /> 
    <asp:Literal runat="server" ID="ltContent" ></asp:Literal>
    </ItemTemplate></asp:TemplateField>
     protected void grLeaveWord_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)
                {
                //判断数据大于50,大于50后面加否则则全显示
                    Literal ltContetn = e.Row.Cells[2].FindControl("ltContent") as Literal;
              if (HFcontent.Value.Length > 50)
                    {
                        ltContetn.Text = content.ToString().PadRight(50).Substring(0, 50) + "....";
                    }
                    else
                    {
                        ltContetn.Text = content.ToString();
                    }
                }
             }
         }
      

  4.   

    在 这个gridview 所在表格  样式中设置   table-layout: fixed;
    固定表格。
    然后在每个头行的列中设置宽度,并设置该列 Wrap=false   不换行
      

  5.   

    1楼的能把那里面的代码给出来一下吗?
    我是用的hyperlinkfield,不是用的模板,我该怎么显示省略号啊 ,各位高手麻烦给出下代码啊!
    小弟在此多谢了
      

  6.   

     //设置不换行  
                GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:False");
      

  7.   

    在RowDataBound事件下写以下代码
    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        if (e.Row.Cells[17].Text.Length > 10)
                        {
                            e.Row.Attributes.Add("style", "word-break:keep-all;word-wrap:False");
                            e.Row.Cells[17].Text = e.Row.Cells[17].Text.Substring(0, 10) + "...";
                        }
                    }