Truncate String in RadGrid or GridView
Posted on: Fri Feb 05 15:10:10 -0800 2010. Updated on: Fri Feb 05 15:43:12 -0800 2010.
Category: Telerik Controls and Sitefinity
Category: Telerik Controls and Sitefinity
Sometimes on display, when we have fields that may contain long text, we want to truncate the max length that is displayed on screen. It is easy to do this. First change the column into a Template, then when binding call a function you define.
// put this method in your code behind
protected string TruncateString(object stringToValidate)
{
string output = Convert.ToString(stringToValidate);
if (output.Length > 30)
{
return output.Substring(0, 30) + "...";
}
else
{
return output;
}
}
Now in your template in the RadGrid, simply do this when outputting the text:
TruncateString(Eval("PartnerNotes"))