DataGridView: Where’s my tooltip?
Posted on December 11, 2005
If you’ve worked with the .NET 1.1 DataGrid control, you’ll appreciate the enhanced DataGridView control of the 2.0 runtime. I was mystified for a moment, however, when setting a tooltip on the grid failed to have any effect. My code looked like this:
DataGridView.HitTestInfo info = grid.HitTest(e.X, e.Y); if (info.RowIndex > -1) { OrdersRow row = (OrdersRow)((DataRowView)grid.Rows[info.RowIndex].DataBoundItem).Row; this.toolTip.SetToolTip(grid, string.Format(“Order {0}, row state {1}”, row.OrderID, row.RowState)); } else { this.toolTip.SetToolTip(grid, null); }
Tooltip.SetToolTip is the standard mechanism for setting a tooltip dynamically. If it’s a static value you want, you can of course set the Tooltip extender property on the grid at design time.
To make a long story short, DataGridView provides a property ShowCellToolTips that when enabled, provides it’s own tooltips in place of any Tooltip component. So, to use a Tooltip component as shown above, set DataGridView.ShowCellToolTips to false.
The built-in tooltips provided by DataGridView allow a great deal of flexibility in providing cell-level tooltips. Without any extra work, setting ShowCellToolTips to true (the default) will cause a tooltip to be displayed if a cell value is truncated because the column is too narrow. You can also set the ToolTipText property on a specific DataGridViewCell to display your own cell-specific tooltip.  Finally, you can handle the DataGridView.CellToolTipTextNeeded event to provide tooltip text dynamically.
Got something to say?