Monday, January 26, 2009

DataGridView Custom Cell ToolTip

Level: Intermediate Knowledge Required:
  • DataGridView Control
  • Windows Forms
Description: We have a built-in ToolTip for each Cell in DataGridView control. Which is displayed when the text is long and cannot be displayed completely in Cell. But my requirement was to create a custom ToolTip (as displayed in above image). There were 2 main requirements, 1) ToolTip should be displayed permanently that is, it should NOT be disappeared automatically after sometime. 2) I want to have a fancy look of my ToolTip (again see the image). For this purpose we can also use the .net's built-in ToolTip control. Which can have a little fancy look by setting some of its properties like IsBubble, ToolTipIcon and ToolTipTitle. Also the ToolTip control can be more customized by using OwnerDraw mode. But still I would like to use my own ToolTip window. Because to me, this is more easier to do. Simply I implemented it by creating a new window for ToolTip and I show this window when mouse enters in a cell. But to make it more user friendly, I have used a Timer control. So that whenever mouse enters in a Cell I start timer and in Timer's Tick Event Handler, I display the ToolTip window. Few things to be considered here, 1) DataGridView's ShowCellToolTips property should be set to False 2) Decide where to display the ToolTip window So in my case I decided to display the ToolTip window just on the Cell. OK this is NOT just straight. We first need to get the Cell's actual Coordinate in DataGridView by executing DataGridView's GetCellDisplayRectangle() function. Then we need to convert these coordinates into Screen's Coordinates by calling the DataGridView's PointToScreen() function. Please note that, to display our own custom ToolTip window on our given position we need to set its property StartPosition = Manual, which I have discussed in my earlier post Setting Window / Form Position Programmatically Another thing I have added in the ToolTip window is the Close Button. Which is actually a NON Focusable button as I have discussed my previous post NOT Focusable / NOT Selectable Button Download Source: DataGridViewCustomCellToolTip.zip

1 comment:

Tanner said...

your sample helped me solve my problem.

All runtime tooltip.show calls are supressed unless the "showcelltooltips" is set to false.

Thanks!