10

I have data bound DataGridView in a desktop app with columns that have their ToolTipText property set, yet no tool tip is displayed when I hover over grid view (cells or cell headers).

The ShowCellToolTips property of the grid view is true, and I have verified using break points that it is not changed programmatically before I mouse over.

I have tried creating a CellToolTipTextNeeded event handler to see what the tool tip text was, but the event handler is never called.

Is there anything I have missed?

Thanks, Rob

Edit: We're using framework 2.0.

davmos
  • 9,324
  • 4
  • 40
  • 43
Robert Gowland
  • 7,677
  • 6
  • 40
  • 58
  • I know this is an old question, but is hacking a work-around with the ToolTip component really the right answer? We're experiencing the same problem with the column/cell tooltip not showing. This seems like a bug in the DataGridView that should get fixed. – Yoopergeek Jan 27 '10 at 14:31
  • @Yoopergeek I agree, it is a bug. I'm told it is fixed in Framework 3.0, but we're not in a position to upgrade due to limitations in our installer. – Robert Gowland Jan 27 '10 at 14:41
  • 1
    I'm using 3.5...still not fixed. ;) – Yoopergeek Jan 27 '10 at 14:55

12 Answers12

9

It appears from your question that you set the tooltip text of the columns. Columns tooltip text only appears when floating over the headers. To show tooltip text on the cells you have to hookup the CellToolTipTextNeeded event and set the value of e.ToolTipText in the event args

davmos
  • 9,324
  • 4
  • 40
  • 43
  • Thanks for the reply, but as stated in the question: a) the tooltip text is not showing on the headers either b) the CellToolTipTextNeeded event handler is not firing – Robert Gowland Jan 08 '09 at 19:27
  • It works for me: private void dgvPlatypus_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) { e.ToolTipText = "j. cutworm is a punk"; } – B. Clay Shannon-B. Crow Raven Sep 14 '12 at 22:52
  • CellToolTipTextNeeded is not getting fired . Any tip for it ? – Viku Oct 24 '13 at 11:54
  • In my case as well it wasn't getting fired but i wrote it and it worked perfect. `this.dataGridView1.CellToolTipTextNeeded += dataGridView1_CellToolTipTextNeeded;` `private void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) { e.ToolTipText = "Trying trying trying"; }` – Vbp Dec 26 '13 at 22:25
4

Try using Cell.ToolTipText property. You will probably need to loop the rows of the DataGridView and set the tooltips manually:

 For Each row As DataGridViewRow In Me.DataGridView.Rows
   Me.DataGridView("MyCol", row.Index).ToolTipText = "MyToolTipText"
 Next

Might not be suitable for a bound DataGridView with lots of rows but works successfully for me with an unbound DataGridView with a couple of hundred rows. Hope this helps.

Simon
  • 6,062
  • 13
  • 60
  • 97
  • 3
    This is the correct answer. From MSDN: "The CellToolTipTextNeeded event occurs only when the DataGridView control DataSource property is set or its VirtualMode property is true." – Kevin McCormick Apr 11 '12 at 16:29
4

When I added a datagridview with a single (empty) column to a form, added text to the ToolTipText property for that column, and ensured that the ShowCellToolTips property for the datagridview is set to True, I do get a tooltip popup when I hover my mouse over that column's header. This seems to contradict what was stated in the original question, but in my test the grid was not data bound. Not sure if that makes a difference. However, on a project with a data bound datagridview, I just used a ToolTip component:

(1) Add a ToolTip component to your form.
(2) Set the ToolTip on toolTip1 (or equivalent name for your ToolTip component) property for your datagridview to whatever text you want to display.
(3) Set your datagridview's ShowCellToolTips property to False.
(4) Viola! Works as expected.

Nick Spreitzer
  • 10,242
  • 4
  • 35
  • 58
4

To show the tooltip of the grid cell, you can use this event handler "CellToolTipTextNeeded". Refer the below code Snippet,

this.dataGridView1.ShowCellToolTips = true;
this.dataGridView1.CellToolTipTextNeeded += dataGridView1_CellToolTipTextNeeded;

void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
    if (e.ColumnIndex >= 0 && e.RowIndex >= 0)           
    {
        e.ToolTipText = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    }
}
Sender
  • 6,660
  • 12
  • 47
  • 66
Muthukumar K
  • 311
  • 1
  • 9
3

I had a simular problem but was able to correct it by setting the ShowCellToolTip to true on my DataGridView. Once I did that I was able to send the following code and everything worked fine.

tableDocTypes.ShowCellToolTips = true;
tableDocTypes.Rows[i].Cells[columnFormCabinet.Index].ToolTipText = "Cabinet is not defined on the optical server.";
JoBaxter
  • 710
  • 2
  • 12
  • 23
3

We ended up using a ToolTip widget and the CellMouseEnter, CellMouseLeave events to show it appropriately. Not optimal, but it works around the odd behaviour we were experiencing.

davmos
  • 9,324
  • 4
  • 40
  • 43
Robert Gowland
  • 7,677
  • 6
  • 40
  • 58
  • I was able to get around using the widget by setting the cells tag to the text in my code where I wanted to set a warning on a specific cell due to values not matching, then using the mouse enter to read the text stored in the cells tag and setting the datagridview cell ToolTipText property to text variable I set from the tag. Then obviously clearing that tag out when the issue is corrected and the mouse enter will act accordingly for empty strings. – Krausladen May 04 '23 at 17:14
2

Set datagridview ShowCellToolTips property to False

Keramat
  • 63
  • 3
  • The opposite worked for me - by setting `dgv.ShowCellToolTips = true;` the column header tooltips then started to display. – Paul Aug 26 '23 at 18:37
1

I'm currently experiencing the same behvoiur on Framework 3.5. Is seems the DataSource property needs to be set in order to get the CelToolTipTextNeeded event to fire.

sindre j
  • 4,404
  • 5
  • 31
  • 32
0
  1. set your DataGridView's ShowCellToolTips property to false
  2. Put this code in your DataGridView's CellMouseEnter event

    private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (!(dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewImageCell))) return;
        System.Windows.Forms.ToolTip tlp = new System.Windows.Forms.ToolTip();
        tlp.SetToolTip(dgv, "Your ToolTipText");
    }
    
Sheridan
  • 68,826
  • 24
  • 143
  • 183
Ghasem
  • 14,455
  • 21
  • 138
  • 171
0

I found this article looking for help on setting tooltips per row.

I just wanted to confirm that handling the CellToolTipText event works for me in VS2008 SP1.

For those of you who are wondering how the set the text to a value from the underlying datarow, this might be useful:

    private void myDGV_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
    {
        // This is used to set tooltiptext for individual cells in the grid.
        if (e.ColumnIndex == 2)  // I only want tooltips for the second column (0-based)
        {
            if (e.RowIndex >= 0)   // When grid is initialized rowindex == 0
            {
                // e.ToolTipText = "this is a test."; // static example.

                DataRowView drv = ((DataGridView)sender).Rows[e.RowIndex].DataBoundItem as DataRowView;
                MyTableRowClass theRow = drv.Row as MyTableRowClass;
                e.ToolTipText = theRow.Col1  + "\r\n" + theRow.Col2;
            }
        }
    }
shindigo
  • 1,267
  • 15
  • 29
  • This might be a better way to test for the correct column. It will still work if you re-arrange the order of the columns. if(myDGV.Columns[e.ColumnIndex].Name == "MyColumn") – shindigo Aug 12 '10 at 17:47
0

I don't know if this tip is a solution to your specific problem, but do you use SP1 of VS2008 ? This Service Pack resolves many different issues, as I have discovered.

Marcus Tik
  • 1,709
  • 6
  • 20
  • 30
  • @WunderWuzzi. No we don't have SP1 installed. I'll talk to the technical lead on the project to see if that's something we can try. I'll update the issue then. Thanks so much. – Robert Gowland Jan 02 '09 at 14:19
0

Had a related problem where CellToolTipTextNeeded would only occasionally be called. The behavior was tooltip over a tooltip when there was overflow in the cell. When WrapMode of the cell is set to true then the CellToolTipTextNeeded would be called correctly every time. I thought that the CellToolTipTextNeeded would be called and override the generic tooltip but it seems to call this event only on the first entry into the datagridview.cell and then, if mouse off the cell and back on (remaining in the datagridview), the "view overflow tooltip" shows instead...

Anyway info might help someone else out.

Andy
  • 1