Link to home
Start Free TrialLog in
Avatar of david_mo
david_mo

asked on

asp.net gridview string length

In gridview I want to restrict the output of a field to a fixed string length.  For example, I want to restrict this field content:
"example text and a lot more more more more more"
to 20 characters, like this:
"example text and a lo..."
How do I do this please?


Avatar of techExtreme
techExtreme
Flag of India image

Hello,
for that you have to use itemtemplate column for your column and use a div inside it and place your content in it. Then you have to do clipping on the div.
Look at this tutorial, explains possible clipping options:
http://www.w3.org/TR/CSS2/visufx.html
This way you can restrict your grid from increasing size according to the text received in column.
Let me know if I misunderstood something.
Hope it helped.
Avatar of david_mo
david_mo

ASKER

Thanks but I don't really understand how this solves it.  I don't want to create a box.  I just want to clip the length of the text in a particular field.  An example of what I want is on Experts Exchange.  If you use the search, the output gives a list of results with the comments field clipped to the same length for each result.
Hi,
Well the easiest way you can go for is to handle your grid's rowdatabound event and fetch the item in a string, then say you want to limit it to 20 (including the three characters '...') then if it exceeds 17 chars, you take first 17 chars, concat with ... and put it in the label  instead of directly binding the dataitem to the label.
This way you can have the same result you want  at max the text will be 20 chars wide.
Thanks but sorry, I did say I was a beginner.  With respect only another expert could understand this.
I'm searching the internet trying to understand the meaning of the words you're using.
ASKER CERTIFIED SOLUTION
Avatar of techExtreme
techExtreme
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial