Last post Apr 06, 2007 01:07 AM by JMayo
Apr 05, 2007 12:00 AM|BGM|LINK
Is there a way to limit the number of displayed text in a gridview field?
Say trim is off at 20 characters? The data still needs to be there, but on the first 20 characters visible to the user...
Apr 05, 2007 01:34 AM|JMayo|LINK
Here's one way:
Given the following page with a GridView:
Title="Untitled Page" %>
You can write a Page_Load method with the following data binding:
protected void Page_Load(object sender,
string myData =
GridView1.DataSource = myData;
And then capture the RowDataBound event like this:
Apr 05, 2007 03:05 AM|BGM|LINK
That looks to be along the right sort of lines, but i do not understand this line:
if (e.Row.RowType == DataControlRowType.DataRow)
perhaps it would help if i explained what i was trying to do a bit further.. maybe someone will come up with a better approach
In the admin area of my application i have an section where an admin can edit HTML and save it to the database. This all works fine, but am trying to do a kind of 'preview' of the HTML, before they select which one to edit.
Now, because the inputted HTML is quite long.. it causes havoc with the rows of the gridview i am using.. hence the need to limit the characters viewed, but still access the data, without going to the db again, gridview1.selectedrow.cells etc etc ???
thank you! :)
if i can't get this to work ill just hide the field or something..
Apr 05, 2007 03:35 AM|limno|LINK
You can use a TemplateField for your column and show your text this way:
Apr 05, 2007 01:08 PM|BGM|LINK
that dosent seem to work limno, i get an error at the .length part, and try as i might don't understna dhow to fix it
Apr 05, 2007 01:19 PM|BGM|LINK
i have changed my approach now, and just made the column hidden :)
thank you for your efforts
Apr 05, 2007 02:02 PM|limno|LINK
Here is the correct version:
Apr 05, 2007 02:36 PM|BGM|LINK
i'm sorry but that does not output anything to the page, the field is just blank :(
could it be because i have HTML code in the field ?
Apr 05, 2007 03:03 PM|limno|LINK
I don't know why your page is blank. You can test some other text to see what causes the problem.
Another way to do this is to use a helper function:
protected string getMore(object longTitle)
String shortTitle = string.Empty;
if (Convert.ToString(longTitle).Length > 10)
shortTitle = Convert.ToString(longTitle).Substring(0, 10);
else if (longTitle is DBNull)
shortTitle = string.Empty;
shortTitle = Convert.ToString(longTitle);
Apr 06, 2007 01:07 AM|JMayo|LINK
The RowDataBound event is called for every row in the grid. Therefore, you'll want to make sure you are working with a normal row that contains data, rather than a header or footer.