Skip to content

DataGridView Tricks #2: Set Tooltip on Individual Cell or Entire Row

Here’s another gem if you wish to display ancillary data in a tooltip to your users, this method can be used to display varying information dependant on which cell they have hovered over, or could be used to display a tooltip over the entire row, no matter what cell they are on.

You would probably only need help when performing data binding, as for standard cell tooltips, just check out the ToolTipText property of a DataGridViewCell.

So what I did was subscribe to the “DataBindingComplete” event:

this.myDataGridView.DataBindingComplete += new System.Windows.Forms.DataGridViewBindingCompleteEventHandler(this.myDataGridView_DataBindingComplete);

Then you would probably want to construct your event handler something like this:

private void myDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in ((DataGridView)sender).Rows)
    {
	foreach (DataGridViewCell cell in row.Cells)
	{

	    // Where 'myModelClass' is the name of your current data source model class in question for this row.

	    DisplayCellTooltip(cell, (myModelClass)cell.OwningRow.DataBoundItem);
	}
    }
}

private void DisplayCellTooltip(DataGridViewCell cell, myModelClass item)
{
    cell.ToolTipText("Testing, 1..2..3! " + item.Name);
}

Or whatever. Enjoy.

I was previously using an example based on this MSDN. article, but the tooltip is continually redrawn on clicks and anytime the cell is required to be reformatted.  This may be good if you have a tooltip whose data needs to change based on events occuring in a grid, but very unlikely.  We must thank MSDN for stearing the general public in the wrong direction, it is a terrible title to name their article and they should be ashmed, and I was too busy to consider they may not have selected the most appropriate handler.

VN:F [1.9.10_1130]
Rating: 3.3/5 (13 votes cast)
VN:F [1.9.10_1130]
Rating: +3 (from 3 votes)
DataGridView Tricks #2: Set Tooltip on Individual Cell or Entire Row, 3.3 out of 5 based on 13 ratings
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

6 Comments

  1. Thank you very much, Graham!
    This tip made the UI for my little project much nicer and more intuitive, without resorting to ugly hacks.

    VA:F [1.9.10_1130]
    Rating: 3.0/5 (2 votes cast)
    VA:F [1.9.10_1130]
    Rating: +1 (from 1 vote)
    Posted on 12-Apr-10 at 4:12 am | Permalink
  2. Wayne

    Nice one, helped me out alot. thx

    VA:F [1.9.10_1130]
    Rating: 1.0/5 (1 vote cast)
    VA:F [1.9.10_1130]
    Rating: +1 (from 1 vote)
    Posted on 19-Aug-10 at 8:04 pm | Permalink
  3. Narasimman

    ;-) Really cool

    VA:F [1.9.10_1130]
    Rating: 3.0/5 (2 votes cast)
    VA:F [1.9.10_1130]
    Rating: +1 (from 1 vote)
    Posted on 11-Jan-11 at 7:25 pm | Permalink
  4. Robert Schmalz

    Just wanted to say thanks for your clear concide code. You saved me a couple of hours of hunting, testing, trial and error, etc.

    VA:F [1.9.10_1130]
    Rating: 3.7/5 (3 votes cast)
    VA:F [1.9.10_1130]
    Rating: +1 (from 1 vote)
    Posted on 26-May-11 at 5:47 am | Permalink
  5. Al

    Man, you gotta change these colors! Poor design!

    VA:F [1.9.10_1130]
    Rating: 3.0/5 (2 votes cast)
    VA:F [1.9.10_1130]
    Rating: +1 (from 1 vote)
    Posted on 04-Jun-11 at 11:52 am | Permalink
  6. Graham O'Neale

    Awesome design!

    VN:F [1.9.10_1130]
    Rating: 3.0/5 (2 votes cast)
    VN:F [1.9.10_1130]
    Rating: +1 (from 1 vote)
    Posted on 06-Jul-11 at 10:35 am | Permalink

2 Trackbacks/Pingbacks

  1. [...] http://goneale.com/2009/05/26/datagridview-tricks-2-set-tooltip-on-individual-cell-or-entire-row/ [...]

  2. [...] http://goneale.com/2009/05/26/datagridview-tricks-2-set-tooltip-on-individual-cell-or-entire-row/ [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...