Wednesday, March 28, 2012

Trimming text returned from db

Hi

In my App I have a datagrid with a template column. In my DB I have a field called "Bio" wich holds user biographies. The idea is that within the datagrid only a small portion of the bio is shown and visitors click onto detail pages to see photgraphs and full information. Is there a way to limit the output from the Bio field to say 100 characters

Thanks

hpYou could limit the size of what you display in your datagrid column for Bio through many ways. One of those is to limit what you fetch from the database using LEFT(Bio,100) in your SQL statement.

You could equally use a bound column. This is what MSDN has to say on that:

"A bound column (BoundColumn element) displays information from one column of a data source. You typically add one bound column to the grid for every data column you want to display. Use bound columns when you want to choose which data columns to display in the grid. Bound columns also allow you to set a variety of properties to specify features such as:

The text, font, and color of the column header and footer.
Column width.
Data format.

Whether the column is read-only (that is, whether it will display an editable control when the row is in edit mode).
For details on creating bound columns in the Visual Studio Web Forms Designer, see Adding Bound Columns to a DataGrid Web Server Control. For details on creating bound columns, see DataGrid Web Server Control."

You should be able to combine both approaches to evolve something nice.
In addition if you need the full bio from the db to use later one (perhaps they can vie the full bio via a link or something), you may want to just limit it on the front end. To do this you could use the ItemDataBound of the datagrid. If the value is stored in a control such as a lable you would use FindControl and then set the value. Something like this...

Label lblTemp = (Label)e.Item.FindControl("lblBio");
if (lblTemp != null)
lblTemp = lblTemp.Text.Substring(1, 100);

May not be completely accurate but it should get you on the right path.
Excellent, Many thanks

0 comments:

Post a Comment