Wednesday, October 30, 2013

How to Truncate a String in a Telerik Grid (RadGrid) Column

Hi all, this small blog post is to show how you can truncate a string in a telerik grid when the string is too long to be shown inside the column. Truncation should be done at the ItemDataBound event of telerik grid.

Following is the column template :

<telerik:GridBoundColumn UniqueName="Column1" Visible="true" ShowSortIcon="true">
<ItemStyle Wrap="true" />                   

</telerik:GridBoundColumn>

The ItemDataBound Column will look like this. 

protected void Grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{

if (e.Item is GridDataItem)
{
GridDataItem dataBoundItem = e.Item as GridDataItem;
if (dataBoundItem["Column1"].Text.Length > 10)
       {
        dataBoundItem["Column1"].Text = dataBoundItem["Column1"].Text.Substring(0, 10) + "...";
       }
}

}



Hope this helps J
Thank You J

1 comment: