Displaying a Long Description Column in a GridView

A lot of times, you have something like a description column and you want to display it in a GridView.  But since for some of the rows the description column can be very long, it doesn’t always work very well. One solution is where you set the TD’s attribute NOWRAP, then the column runs off to the right for as long as is needed.  The solution I sometimes employ is the following: 

First, here is my sample table:

CREATE TABLE Categories
(CategoryName varchar(20),
Description varchar(1000))
And here is the SQL that is going to bind to my GridView:
SELECT c.CategoryName,
CASE WHEN LEN(c.Description) > 30
THEN SUBSTRING(c.Description,1,27)+'...'
ELSE c.Description END AS LineDescription,
c.Description AS FullDescription
FROM dbo.Categories c
ORDER BY c.CategoryName

Notice that the Description column is in there twice. Once where it maxes out at 30 characters (the LineDescription column), and the second one that contains the full text (the FullDescription column.) This brings back a table that looks like the following:

 
CategoryNameLineDescriptionFullDescription
BeveragesSoft drinks, coffees, teas,…Soft drinks, coffees, teas, beers, and ales
CondimentsSweet and savory sauces, re…Sweet and savory sauces, relishes, spreads, and seasonings
ConfectionsDesserts, candies, and swee…Desserts, candies, and sweet breads
Dairy ProductsCheesesCheeses
Grains/CerealsBreads, crackers, pasta, an…Breads, crackers, pasta, and cereal
Meat/PoultryPrepared meatsPrepared meats
ProduceDried fruit and bean curdDried fruit and bean curd
SeafoodSeaweed and fishSeaweed and fish

So, to bind this to the GridView we bind the first two columns only.  The first GridView column is bound to the CategoryName data column, and the second one is bound to the LineDescription data column with the heading title of “Description”.  In the second GridView column, we also click the “Convert this field into a TemplateField” as shown below.

gridviewcolumns thumb

We save this and then edit the templates for the GridView columns.  For the Description column, we edit the ItemTemplate and we bind the ToolTip to the FullDescription data column.

tooltipbinding thumb

Now we have the GridView column that is titled “Description” bound to the LineDescription data column, and the tooltip is bound to the FullDescription data column.  We can run this and  see how it works:

gridviewwithtooltip thumb

So now we have a short description in our grid, but the user can easily see the full description in the ToolTip.

Leave a Comment

Your email address will not be published. Required fields are marked *