2010-01-19 52 views
1

使用Telerik RadGrid *在LINQ上下文中使用ASP.NET/C#,如何在列中顯示時將文本截斷爲最大長度?最大限度地說,如果原始字符串的長度小於指定的最大長度,則不會出現錯誤。截斷在RadGrid列中的文本

我在網上看到過很多這樣的例子,但是看起來使用LINQ的Container.DataItem用於實現這一點不同。有時我們會看到DataItem作爲一種方法,有時候不會。示例通常使用DataSet

這裏的發現(source)的例子:

<asp:TemplateField HeaderText="Description"> 
    <ItemTemplate> 
    <%# ValidateString(Container.DataItem("Description")) %> 
    </ItemTemplate> 
</asp:TemplateField> 

而隱藏代碼:

protected string ValidateString(object String) 
{ 
    if ((String.ToString().Length > 50)) 
    { 
    return String.ToString().Substring(0, 50) + "..."; 
    } 
    else 
    { 
    return String.ToString(); 
    } 
} 

謝謝你的幫助。

(*)或者一個正常的GridView,應該是兼容的。

回答

3

我會掛鉤OnItemDataBound事件,並在您的代碼後面執行此操作。鑄數據項作爲您的自定義對象和查詢物業,像下面這樣(輸入一些從內存中):

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
{ 
    if (e.Item is GridDataItem) 
    { 
      GridDataItem dataBoundItem = e.Item as GridDataItem; 
      CustomObject o = e.Item.DataItem as CustomObject; 

      if(o.Description.Length > 50) 
      { 
       dataBoundItem["Description"].Text = o.Description.Substring(0, 47) + "..." 
      } 
     } 
} 

另外,如果你想堅持使用的是嘗試在你的aspx

以下的方法
<telerik:GridTemplateColumn> 
    <ItemTemplate> 
      <%# ValidateString(Eval("Description").ToString()) %> 
    </ItemTemplate> 
</telerik:GridTemplateColumn> 
1

使用ItemDataBound事件的RRRR的想法是一個很好的領先優勢。但是,通過使用內部聯接的自定義查詢來填充我的網格,我無法使用CustomObject屬性。我通過直接修改其文本來使用dataBoundItem["ColumnName"]。事實上,解決方案很簡單!

下面是一個例子:

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
{ 
    if (e.Item is GridDataItem) 
    { 
     GridDataItem dataBoundItem = e.Item as GridDataItem; 
     if (dataBoundItem["ColumnName"].Text.Length > 100) 
     { 
      dataBoundItem["ColumnName"].Text = dataBoundItem["ColumnName"].Text.Substring(0, 100) + "..."; 
     } 
    } 
} 

感謝的方式RRRR

0

使用轉換器也適用於此。

<UserControl.Resources> 
    <myConverters:SubStringConverter x:Key="First50CharactersConverter" Length="50" ShowEllipse="True" /> 
</UserControl.Resources> 
... 
<telerik:GridViewDataColumn DataMemberBinding="{Binding Comments, Converter={StaticResource First50CharactersConverter}}" Header="Comments"> 

轉換器子類:

/// <summary> Mimics the behavior of string.substring for use in XAML </summary> 
public class SubStringConverter : IValueConverter 
{ 
    /// <summary> the zero-based starting character position </summary> 
    public int StartIndex { get; set; } 

    /// <summary> The number of characters in the substring </summary> 
    public int Length { get; set; } 

    /// <summary> shows "..." if value was truncated after StartIndex</summary> 
    public bool ShowEllipse { get; set; } 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     string valueString = value as string; 
     if (string.IsNullOrWhiteSpace(valueString) == false) 
     { 
      if (Length > 0 && Length < (valueString.Length + StartIndex)) 
      { 
       if (ShowEllipse) 
        return valueString.Substring(StartIndex, Length - 3) + "..."; 
       else 
        return valueString.Substring(StartIndex, Length); 
      } 
      else if (StartIndex < valueString.Length) 
       return valueString.Substring(StartIndex); 
      else 
       return ""; //because startIndex must be past the length of the string 
     } 
     else 
     { 
      return value; 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
0

你的代碼是好的。只需將「Container.DataItem」更改爲「Eval」即可。並保持一切如實。然後嘗試一下。我希望這將工作... 所有賭注

現有: <%#ValidateString(的Container.DataItem( 「說明」))%>

變化: <%#ValidateString(Eval(「Description」))%>