truncate the extra text in column and append "..." to truncated string

5 posts, 0 answers
  1. prayag ganoje
    prayag ganoje avatar
    127 posts
    Member since:
    Feb 2010

    Posted 27 Apr 2011 Link to this post

    Hello,

    I am do have data extending the column width. But I want to row without data wrap and truncated row should displayed with "..." extension. I do have a dataset as a datasource for the grid. Is it possible with some string format to achieve this ? Or can you suggest some better method to do this ?

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Apr 2011 Link to this post

    Hello Prayag,

    In order to achieve this, check for the Length of the cell text and then set ToolTip. Try the following snippet code by attaching ItemDataBound event to your RadGrid. Hope this helps you.

    C#:

    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
       if (e.Item is GridDataItem)
      {
      GridDataItem item = (GridDataItem)e.Item;
      if (item["ColumnUniqueName"].Text.Length > 4)
       {
       item["ColumnUniqueName"].ToolTip = item["ColumnUniqueName"].Text.ToString(); // Set the Tooltip
       item["ColumnUniqueName"].Text = (item["ColumnUniqueName"].Text).Substring(0, 5) + "...";
      }
     }
    }

    Thanks,
    Princy.

  3. Derek
    Derek avatar
    110 posts
    Member since:
    Oct 2008

    Posted 25 Sep 2011 Link to this post

    Any code examples for how to accomplish this when using a column containing HTML? There is a column containing RadEditor content, so it's not as simple as just truncating the content since the content contains html content as well as actual text.

  4. Jesse
    Jesse avatar
    19 posts
    Member since:
    Jun 2014

    Posted 01 Jul Link to this post

    Sorry to resurrect this, but I haven't been able to find a great answer for this question.

    I have an option to remove columns in my radgrid, so the user can choose how many columns they want to show. If they show all columns, I'd want to truncate the text much earlier than if only one column is being shown.

    Would I have to do something like this?

    Telerik.WebControls.GridDataItem item = (Telerik.WebControls.GridDataItem)e.Item;
    int numberOfCharacters = (int)(item["ColumnUniqueName"].Width.Value / 9); // 9 being an arbitrary number of about how many pixels per character
    if (item["ColumnUniqueName"].Text.Length > numberOfCharacters)
    {
        item["ColumnUniqueName"].ToolTip = item["ColumnUniqueName"].Text; // Set the Tooltip  
        item["ColumnUniqueName"].Text = (item["ColumnUniqueName"].Text).Substring(0, numberOfCharacters + 1) + "...";
    }


  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 02 Jul in reply to Jesse Link to this post

    Hi Jesse,

    Yes you can use the above code to truncate the Text.

    C#:

    item["ColumnUniqueName"].Text = (item["ColumnUniqueName"].Text).Substring(0, 9) + "...";

    Another way to obtain ellipsis is using CSS as shown below:

    CSS:
    .RadGrid td
    {
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    Thanks,
    Princy

Back to Top