views:

655

answers:

2

I need to apply formatting - specifically, bold text - to the tooltip used by a DataGridView in virtual mode. I can set the text in the CellToolTipTextNeeded event, but it doesn't support HTML tags; is there some other syntax I should be using? I don't want to have to reimplement tooltip support myself.

A: 

As the name of the event implies, it simply wants the text to display, which will not have formatting.

If you want anything like bold, or other kinds of formatting, you are going to have to handle the display and drawing of the tool tip yourself. You can use the ToolTip control to help, setting the OwnerDraw property to true and handling the Draw event, but note, you will most likely have to override the grid in a significant manner to get into that event at the appropriate time.

casperOne
+1  A: 

You can use the HtmlToolTip from http://www.codeproject.com/KB/GDI-plus/HtmlRenderer.aspx

To use it with DataGridView create a ToolTip (HtmlToolTip) and add this after the InitalizeComponent() in your form to replace the default tooltip:

System.Reflection.FieldInfo toolTipControlFieldInfo=
typeof(DataGridView).GetField("toolTipControl", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

System.Reflection.FieldInfo toolTipFieldInfo=
toolTipControlFieldInfo.FieldType.GetField("toolTip", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

object toolTipControlInstance =
toolTipControlFieldInfo.GetValue(myDataGridView);

toolTipFieldInfo.SetValue(toolTipControlInstance, myToolTip);

Work for me with .net 3.5