limit character length in gridview cell

Posted on 2008-11-03
Last Modified: 2009-04-13
I have a field in the database that have up to 1000 characters in it.  In my gridview I only want to show the first 100 characters in that column, but when they click the view/edit link then it will show all the text.

Does anyone have a simple way of doing this?  Thanks.
Question by:andy12279
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 53

Expert Comment

ID: 22867570
Perhaps this example can help you: Dynamic Gridview - Adding a max length to the edit textbox
LVL 26

Accepted Solution

Anurag Thakur earned 125 total points
ID: 22869553
quite simple
add a template filed for description and code as shown belwo
<asp:TemplateField ItemStyle-HorizontalAlign="left" ItemStyle-Width="170px" HeaderText="Description" >
		<%# Eval ("Description").ToString ().Length > 100 ? Eval ("Description").ToString ().Substring (0, 100) + "&" : Eval("Description").ToString ()%>

Open in new window

LVL 22

Expert Comment

ID: 22890716
Even simpler:
<asp:TemplateField ItemStyle-HorizontalAlign="left" ItemStyle-Width="170px" HeaderText="Description" >
		<%# Eval ("Description").ToString ().Substring (0, 100)%>

Open in new window


Author Comment

ID: 22890738
Yes prairidog that will work but if the string is less than 100 characters it will throw an error.
LVL 22

Expert Comment

ID: 22890788
Really? I thought it will just ignore the second parameter if the length is less than 100. Well, I was wrong.

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to Interpret Json in ( .net 3.5  framework) 11 50 mvc5 6 32
Pdf not loading 2 39
search by any word in column 5 29
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question