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.
6 Comments
Thank you very much, Graham!
This tip made the UI for my little project much nicer and more intuitive, without resorting to ugly hacks.
Nice one, helped me out alot. thx
Just wanted to say thanks for your clear concide code. You saved me a couple of hours of hunting, testing, trial and error, etc.
Man, you gotta change these colors! Poor design!
Awesome design!
2 Trackbacks/Pingbacks
[...] http://goneale.com/2009/05/26/datagridview-tricks-2-set-tooltip-on-individual-cell-or-entire-row/ [...]
[...] http://goneale.com/2009/05/26/datagridview-tricks-2-set-tooltip-on-individual-cell-or-entire-row/ [...]
Post a Comment