using Telerik RadGrid* in a LINQ context, with ASP.NET/C#, how to truncate text to a maximum length when displaying in columns? By maximum, I mean if original string’s length is shorter than the specified maximum length, no errors will raise.
I have seen many examples of this on the net, but it seems that the Container.DataItem used to achieve this is different when using LINQ. Sometimes we see DataItem as a method, sometimes not. Examples are usually using a DataSet.
Here’s an example found (source) :
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%# ValidateString(Container.DataItem("Description")) %>
</ItemTemplate>
</asp:TemplateField>
And codebehind:
protected string ValidateString(object String)
{
if ((String.ToString().Length > 50))
{
return String.ToString().Substring(0, 50) + "...";
}
else
{
return String.ToString();
}
}
Thank you for the help.
(*) Or a normal GridView, supposed to be compatible.
I would hook into the OnItemDataBound event and perform this in your code behind. Cast the data item as your custom object and query the property, something like the following (typing some from memory):
Alternatively if you want to stick with the method you are using try the following in your aspx