6 Replies - 2979 Views - Last Post: 21 August 2012 - 03:18 PM Rate Topic: -----

#1 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,676
  • Joined: 24-August 11

Dynamically generating tooltips in GridView

Posted 13 August 2012 - 01:45 PM

I have a GridView that displays metadata about documents in our DMS. The document titles tend to get pretty long, so I cut them off after a certain number of characters, but users still need to be able to view the entire title. So I added logic to dynamically generate ToolTips for the cells to display the full title. Documents are modeled as objects, and are passed in a List<Document>.

protected void GvDisplayDocuments_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(gvDisplayDocuments,
                                                                                "Select$" + e.Row.RowIndex);
        var lastCell = e.Row.Cells.Count - 1;
        var index = e.Row.RowIndex;

        e.Row.Cells[lastCell].ToolTip = _docList[index].DrawingTitle;
    }
}



This worked perfectly for a vanilla GridView, but failed once I turned paging on, so I modified index to:

        var recordsPerPage = myGridView.PageSize;
        var index = (recordsPerPage * _currPage) + e.Row.RowIndex;



where _currPage is set from the PageIndexChanging event to e.NewPageIndex. This works for paging now, but turning on Sorting kills it because the row index doesn't change OnSorting, so the ToolTips still reflect the original sort order. I couldn't figure out how to attach the full title to the cell and only display a specific number of chars/words, so I don't know how to fill the tooltip other than by matching row index to the list index. Anyone have any ideas?

NOTE: Sorting isn't a project requirement (and wasn't available in the previous incarnation of the site), it's just a feature I think we should provide, so if there aren't any semi-elegant solutions I'll just can it.

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically generating tooltips in GridView

#2 code_junkie83  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 62
  • Joined: 17-June 09

Re: Dynamically generating tooltips in GridView

Posted 14 August 2012 - 10:47 AM

Could you possibly use a hidden cell in the row to store the tooltip text, and then read that data to fill the tooltip?
Was This Post Helpful? 1
  • +
  • -

#3 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,676
  • Joined: 24-August 11

Re: Dynamically generating tooltips in GridView

Posted 14 August 2012 - 11:53 AM

I could, and that will be what I do if I don't find a solution I like more (if sorting becomes a requirement). But I don't want to pollute my GridView (or the form in general) with a bunch of hidden fields holding data that I already have in memory.
Was This Post Helpful? 0
  • +
  • -

#4 code_junkie83  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 62
  • Joined: 17-June 09

Re: Dynamically generating tooltips in GridView

Posted 14 August 2012 - 12:09 PM

Is it possible to generate the tooltip data using some form of a key from the data? You could store the data in a dictionary using key value pairs, then pull based on the key. I don't know if that owuld alleviate the clutter or not, seems to play along with what you have in place ATM.
Was This Post Helpful? 1
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Dynamically generating tooltips in GridView

Posted 14 August 2012 - 12:46 PM

Using a List<T> on a GridView can make for a lot of pain when trying to implement sorting. You could look into creating your own Func<type, object> expression to pass into the orderby/orderbydescending methods. I haven't personally had much look with this method but I'm not real familiar with building expressions. Another option is to use the Dynamic Linq library discussed by Scott Guthrie here.

You'll need to store the sort column and direction in some manner. I usually use viewstate backed properties for this. Use the onsorting event to update these values. The column will be passed into the sorting event as the SortExpression. The SortDirection however will always be ascending, so you'll have to just check the properties existing value and change it accordingly. I'll try provide some code examples later if needed.
Was This Post Helpful? 1
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,676
  • Joined: 24-August 11

Re: Dynamically generating tooltips in GridView

Posted 15 August 2012 - 11:10 AM

@CodeJunkie - That still leaves me replicating the data though. Both of your suggestions are viable and would work, I'm just being really stubborn about what I'll implement since it isn't officially a requirement (it didn't even make the feature list).

@Nakor - Yeah, I'm figuring that out about GridViews and Lists. It's been really helpful for all of my data manipulation, and is great for initially displaying in the GridView. I'll take a look at that Scott Guthrie link you provided and play around with a few other things.

Thanks for your help gents, I'll try a few things and post whatever solution I end up using.
Was This Post Helpful? 0
  • +
  • -

#7 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,676
  • Joined: 24-August 11

Re: Dynamically generating tooltips in GridView

Posted 21 August 2012 - 03:18 PM

Just as a follow-up, I kind of went with your suggestion, CodeJunkie. I created another BoundField, used CSS to make it not visible (if you use the BoundField's visibility attribute, it won't contain the data), and set the text size to 1. Kind of jinky, but oh well. It actually had nothing to do with the sorting, although sorting is now implemented as a result.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1