vb.netdatagridviewtooltipdatagridviewcombobox

vb.net show tooltiptext for every item in datagridviewcombobox


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.


Solution

  • 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