Home > ASP.Net > Limit Text Length in Gridview

Limit Text Length in Gridview


try this example:

<asp:TemplateField HeaderText="Description">
          <asp:Label ID="lblDescription" runat="server"
                    Text='<%# Limit(Eval("Description"),40) %>' >

The helper function:

public static string Limit(object Desc, int length)
        StringBuilder strDesc = new StringBuilder();
        strDesc.Insert(0, Desc.ToString());

        if (strDesc.Length > length)
            return strDesc.ToString().Substring(0, length) + "...";
        else return strDesc.ToString();

Hope it helps

Good Luck

Categories: ASP.Net
  1. sergeos
    February 7, 2009 at 12:11 pm

    wonderful solution, but can you say detailed where i must put the helper function?

  2. yasserzaid
    February 7, 2009 at 2:05 pm

    Thanks sergeos for your comment

    i use the helper function with a label control i add in ItemTemplate of my Gridview control to limit Text displayed in my lable control

    Hope this helps you

    Good Luck

  3. Keshav
    June 30, 2013 at 6:24 am

    Thanks amazing solution grate job.

  4. yasserzaid
    July 8, 2013 at 1:37 pm

    @Keshav : you are welcome

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: