请问一些datagrid中的item的text过长,比如我只想显示width=200px,“中华人民共”这几个字正好200px的宽度,但是超过这个宽度就以...显示,如“中华人民共和国”就显示为“中华人民共...”
请问怎么做呢?

解决方案 »

  1.   

    在你的sql语句中可以使用substring函数把字符按一定的长度截断!
      

  2.   

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
      ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
          If e.Item.Cells(0).Text.Length > 26 Then
            e.Item.Cells(0).Attributes.Add("Title", e.Item.Cells(0).Text)
            e.Item.Cells(0).Text = e.Item.Cells(0).Text.Substring(0, 26) + "…"
          End If
          e.Item.Cells(1).Text = Format(System.Convert.ToDateTime(e.Item.Cells(1).Text),_
           "yyyy年M月d日 h点m分s秒")
        End If
      End Sub
      

  3.   

    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType==ListItemType.EditItem)
    {
    for (int i = 0;i<16;i++)
    {
    TextBox textBox = (TextBox)e.Item.Cells[i].Controls[0];
    textBox.Width = 200%;
    }
    }
    }
      

  4.   

    最好在SQL直接截断.省得下载数据了.
      

  5.   

    可以在前台绑定数据的时候换成绑定方法
    然后在后台写方法,实现截断字符串
    前台<%#getString((string)(DataBinder.Eval(Container.DataItem,"title")),38)%>
    后台: protected string getString(string StringInput,int StringLength)
    {
    if(StringInput==""||StringInput==null)
    return "";
    string tempTitle="";
    if(StringInput.Length>StringLength)
    tempTitle = StringInput.Substring(0,StringLength)+"..."; 
    else if(StringInput.Length==StringLength)
    tempTitle = StringInput;
    else
    {
    for(int i=0;i<StringLength-StringInput.Length;i++)
    {
    StringInput = StringInput+"&nbsp;";
    tempTitle=StringInput;
    }
    }
    return tempTitle; 
    }
      

  6.   

    StringInput.Length 
    将一个汉字长度看作1,一个英文字母长度也是1...........有什么函数能将一个汉字长度看作2的,英文一个才是1 ?汉字占的位置可是英文字母的两倍啊~
      

  7.   

    byte[] buf = (byte[])System.Text.Encoding.Default.GetBytes(e.Item.Cells[cellIndex].Text);
    if(buf.Length > 30)
    {
    e.Item.Cells[cellIndex].Text = System.Text.Encoding.Default.GetString(buf, 0, 30 - 1) + "...";
    }
      

  8.   

    不建议用截取,可以用, <ItemTemplate>
    <FONT face="宋体">
    <asp:TextBox id=TextBox5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.fcontactname") %>' CssClass="myinput">
    </asp:TextBox></FONT>
    </ItemTemplate>