2011-03-08 21 views

回答

5

您可以處理在GridView的RowDataBound事件和剪切的文本長度,就像這樣:

protected void gvNotes_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowIndex < 0) 
     return; 

    int _myColumnIndex = 0; // Substitute your value here 

    string text = e.Row.Cells[_myColumnIndex].Text; 

    if (text.Length > 100) 
    { 
     e.Row.Cells[_myColumnIndex].Text = text.Substring(0, 100); 
    } 
} 
+0

我遇到了數據綁定問題:s 如果你能看到你會笑的數據發生了什麼。 我得到的數據全部在我的屏幕上:s – SamekaTV 2011-03-09 07:13:32

+0

我不想減小字符串的大小。我只需要在gridview – SamekaTV 2011-03-09 07:30:42

0

對於IE只能回答,您可以使用CSS和,只要你有一組寬度該列,設置溢出:省略號或溢出:隱藏(應適用於所有瀏覽器),如果你不想要點。


OK,按該意見,我沒有用GridView的一段時間,但它會設置一個CSS類爲每個細胞,並在類的事:

trimText 
{ 
    overflow:ellipsis; 
} 

還有一些黑客,你可以做的就是這顯示跨瀏覽器 - 一些注意事項這裏:

http://www.jide.fr/english/emulate-text-overflowellipsis-in-firefox-with-css

+0

中顯示一小部分數據,這正是我想要做的。我該怎麼做到這一點 – SamekaTV 2011-03-09 07:28:05

0

創建此功能

public object TrimString(string input, int length) 
    { 
     // return nothing if the string is null 
     if (String.IsNullOrEmpty(input)) 
     { 
      return string.Empty; 
     } 

     // invalid length submitted 
     if (length <= 0) 
     { 
      length = 100; 
     } 

     if (input.Length > length) 
     { 
      return input.Substring(0, length) + "..."; 
     } 

     return input; 
    } 

你可以從你的aspx頁面調用它。

<ItemTemplate> 
     <asp:Label ID="Label4" runat="server" Text='<%# TrimString(Eval("CustName").ToString(),100) %>'></asp:Label> 
</ItemTemplate> 
相關問題