1

I have searched for displaying tooltiptext for datagridview but I'm only getting tooltiptext for datagridviewcell. What I want is the tooltiptext to display when an item is highlighted (mouse hover) from the dropdown list of a datagridviewcomboboxcolumn. I have set the tooltiptext in databinding of the comboboxcolumn but it doesn't display anything in runtime.

'assuming dtExpense is the datatable used as datasource
With CType(DataGridView3.Columns(2), DataGridViewComboBoxColumn)
    .AutoComplete = True
    .DataSource = dtExpense
    .DisplayMember = "acct_title"
    .ValueMember = "acct_id"
    .DataPropertyName = "acct_id"
    .ToolTipText = "description"
End With

Can anyone tell me how to do this. In datagriviewcell.tooltiptext it has to drawn at some point. I was thinking how to do this with datagridviewcomboboxcolumn and it has to display for each item in the combobox.

1 Answers1

0

Assuming you have an object class with string properties named acct_title (to display as the dropdown items) and description (to display as tooltips on those dropdown items), you'll want to:

  1. Add a ToolTip control to your Form.
  2. Handle the EditingControlShowing event for the DataGridView to add event handlers to the underlying ComboBox.

    Me.DataGridView3.EditingControlShowing += DataGridView3_EditingControlShowing
    
    Private Sub DataGridView3_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
    If TypeOf e.Control Is ComboBox Then
            Dim cb As ComboBox = TryCast(e.Control, ComboBox)
            cb.DrawMode = DrawMode.OwnerDrawFixed
            cb.DrawItem -= Cb_DrawItem
            cb.DrawItem += Cb_DrawItem
            cb.DropDownClosed -= Cb_DropDownClosed
            cb.DropDownClosed += Cb_DropDownClosed
        End If
    End Sub
    
  3. Handle the ComboBox DrawItem event to set the ToolTip value and show it. We will use reflection to grab the description property off the dropdown items and set it's string value to the ToolTip.

    Private Sub Cb_DrawItem(sender As Object, e As DrawItemEventArgs)
        Dim cb As ComboBox = TryCast(sender, ComboBox)
        Dim item = cb.Items(e.Index)
        Dim display As String = cb.GetItemText(item)
        Dim toolText As String = item.[GetType]().GetProperty("description").GetValue(item, Nothing).ToString()
    
        e.DrawBackground()
    
        Using br As New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(display, e.Font, br, e.Bounds)
        End Using
    
        If (e.State And DrawItemState.Focus) = DrawItemState.Focus AndAlso cb.DroppedDown Then 
            Me.toolTip1.Show(toolText, cb, e.Bounds.Right, e.Bounds.Bottom, 2000)
        End If
    
        e.DrawFocusRectangle()
    End Sub
    
  4. Handle the ComboBox DropDownClosed event to hide the ToolTip.

    Private Sub Cb_DropDownClosed(sender As Object, e As EventArgs)
        Dim cb As ComboBox = TryCast(sender, ComboBox)
        Me.toolTip1.Hide(cb)
    End Sub
    
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
  • Thank you for your answer! I needed to change some lines of code since there's a lot of other comboboxes in my datagridview. I think its doing fine. But, I am getting a NullReferenceException around this line `Dim toolText As String = item.[GetType]().GetProperty("description").GetValue(item, Nothing).ToString()` I even check if `cb isNot Nothing` (although I think its not necessary since tooltip should show when item is hovered and not when selected) but this error still persist. Do you think I need to add a datatype around here `Dim item = cb.Items(e.Index)`? – Captain Meelo Jan 29 '16 at 10:17
  • The only way I could replicate that error was if I did `.GetProperty("PropertyThatIsNotOnTheObject")`. You could try putting a data type on `Dim item` but if I'm correct, that will just throw the same error. Make sure `GetProperty` is called with a string that is the name of the property you want to display in your toooltip. – OhBeWise Jan 29 '16 at 15:42
  • I did set `.ToolTipText = "description"` in my datagridviewcomboboxcolumn property. That should be fine right? But I can't really get why I'm still getting NullReference. So, I did a workaround and I kinda search the `description` from the `Datasource` itself. I'm being very inefficient here but it did the work. Thank you for your help!! But there's a little bit of a problem, can you still help me here? The tooltip displays at the very bottom of my screen since I have a very long list of account titles. How can I get it to display beside the combobox item? – Captain Meelo Jan 30 '16 at 02:02
  • Well, I investigated why this happened, I think its because the dropdown transfers to the top if there's not enough space below the combobox itself. And while that happens, `e.Bounds.Bottom` still thinks that the first item is still displayed just below the combobox. What I did is just subtract some amount from it to fix its Y-coordinate. I'm just not sure if this will hold up in difference screen resolutions since its not dynamic enough to adjust itself. – Captain Meelo Jan 30 '16 at 05:44
  • Sorry the tooltip location gave you grief. In my tests it displayed aligned and to the right of the hovered dropdown item, but I'm glad to hear you found a workaround. Sorry for the slow response; I moved over the weekend. – OhBeWise Feb 01 '16 at 15:44