How To Display Balloon Tooltip Message For a Single Row in the Datagridview Using C#?


  • Hi ,

    I need to display the balloon tooltip message in datagridview,For some  rows not for the entire datagrid.

    I have done for the entire datagrid but could'nt find a way on how to set for a single row..

    Who ever Knows Kindly Help Me

    • Moved by Harry Zhu Tuesday, July 07, 2009 7:46 AM (From:Visual C# Express Edition)
    Monday, July 06, 2009 6:50 AM

All replies

  • For a Windows Forms DataGridView, you can handle the
    dataGridView1.CellToolTipTextNeeded event and dynamically provide the
    tip text there.

    dataGridView1.CellToolTipTextNeeded += new
    DataGridViewCellToolTipTextNeededEventHandler(data GridView1_CellToolTipTextNeeded);

    void dataGridView1_CellToolTipTextNeeded(object sender,
    DataGridViewCellToolTipTextNeededEventArgs e)
    e.ToolTipText = string.Format("tip for row {0}, col {1}",
    e.RowIndex, e.ColumnIndex);

    For more info see this thread
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    Monday, July 06, 2009 9:20 AM
  • Thanks For ur Reply,

    But this still does'nt Work can u be a little brief on how to display the balloon tooltip for a single row in datagrudview

    Antony Romar M
    Monday, July 06, 2009 11:18 AM
  • I think I've found a little trick.

    Put a Label on the form. Set its text to " ". ... Label1.Text = " "
    Put the Label behind the DataGrid ... with the "Send to back" menu option.

    In the DataGrid CellToolTipTextNeeded event ... write something like this:

       ' Reposition the Label just behind the current cell.
       Label1.Location= DataGridView1.Location + DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex,False).Location
       ' Show balloon (I suppose you have the Tooltip control in the form and its IsBalloon property set to True) 
       ToolTip1.Show ("Some Text", Label1)
    Catch ex As Exception
    End Try

    ' --------------------------------

    ' If you want the Tooltip just in the mouse position, the only thing you have is to change the Location of the Label.
    ' you can set a Private variable named MouseLocation, and set its value within the DataGridView1.MouseOver....
    MouseLocation = New Point (e.X, e.Y).
    ' then set Label1.Location = MouseLocation + DataGridView1.Location

    Hope it works

    Juan Carlos
    Tuesday, July 07, 2009 7:13 PM
  • Hi Juan,

    Thanks for ur Reply,

    By Placing the label behind the grid will not help, Coz i need to show the balloon tooltip for the entire row that too the row can be in any of the positon, i mean it may be the first row or 2nd or 3rd...........

    All the rows are dynamically loaded for some criteria..

    Kindly Suggest some other Solution...

    Wednesday, July 08, 2009 4:08 AM
  • Sorry...
    I asumed that the DataGrid was Databounded...

    Try this .... Create a Form with a DataGridView, a button, a ToolTip and a Label.  Set Label.Visible = False. (or send it to the back)...
    Set Tooltip.ShowAlways = True (so it will work although its parent control -the label- is not visible) and adjust the delays to suit your needs.
    Also, Tooltip.IsBallon = True...

    Set the DataGridview1.ShowCellToolTips = False.

    Public Class Form1

       ' Structure to define some things. If you use the tag for another purpose, include the necessary field in the structure.
        Private Structure JCTag
            Dim nTag As Integer
            Dim bShowTip As Boolean
            Dim sTag As String
        End Structure

       ' Load the datagrid.... (because it is not databounded). If it is databounded set the ShowCellTooltips = True and use the CellToolTipTextNeeded event...
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
           For i As Integer = 0 To 100

                ' Add a row
    Me.DataGridView1.Rows.Add("C1-" + i.ToString, "C2" + i.ToString)

                ' Set Tooltip only in certain rows (in this example only in the rows that are divisible by 10).     sTag will be the ToolTip text.

                If i Mod 10 = 0 Then
                      DataGridView1.Rows(i).Tag = New JCTag With {.nTag = i, .sTag = i.ToString + " is divisible by 10 ", .bShowTip = True}
                      DataGridView1.Rows(i).Tag = Nothing
                End if

        End Sub

         ' Use the MouseMove event to check and show the tooltip balloon....

        Private Sub DataGridView1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseMove

           ' Previous mouse's row 
           Static OldRow As Integer

            ' Mouse's Row?
            Dim nRow As Integer = DataGridView1.HitTest(e.X, e.Y).RowIndex

            ' Only if mouse is in a different row.
            If OldRow = nRow Then Exit Sub

            ' Save mouse's row
            OldRow = nRow

           ' Only to test everithing goes right (you must see the Form's title showing the mouse's row). Delete after tests....
            Me.Text = nRow

            ' Read tag Structure
            Dim xTag As JCTag = Nothing
            Dim bShowTooltip As Boolean = False
            Dim sTip As String = String.Empty

            ' Show tooltip if: There is associated JCTag + bShowTip is True + sTip is not empty
                xTag = CType(DataGridView1.Rows(nRow).Tag, JCTag)
                sTip = xTag.sTag
                bShowTooltip = (xTag.bShowTip) And (sTip <> String.Empty)
            Catch ex As Exception
            End Try

            If bShowTooltip Then
                  ' There is a tooltip for current row .... 
                 '  Set Label1 position and size just as the current row coordinates and size

                Dim r As Rectangle
                r = DataGridView1.GetRowDisplayRectangle(nRow, True)
                Label1.Location = DataGridView1.Location + r.Location + New Point(DataGridView1.RowHeadersWidth, 0)
                Label1.Size = r.Size - New Size(DataGridView1.RowHeadersWidth, 0)
                 ' Show the tooltip
                ToolTip1.Show(sTip, Label1)
                  ' There is no tooltip for current row .... Hide the Tooltip
                ToolTip1.Show("", Label1)
            End If

        End Sub

    End Class

    I've tried that and it works for me. It only shows the balloon if the mouse is over a row that has a tooltip associated.
    It hides the tooltip when the mouse goes to another row.

    You have to change sligthly the code if the tooltip depends on the cell itself and not on the row.


    Juan Carlos
    Wednesday, July 08, 2009 8:45 AM
  • I forgot ... The datagridview in the sample has two columns....
    Juan Carlos
    Wednesday, July 08, 2009 8:49 AM
  • Thanks Juan Carlos,

    Still the balloon tooltip is not working as expected.

    Thursday, July 09, 2009 8:01 AM