DataGridView ToolTipText not showing
Asked Answered
B

12

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.

Banquer answered 23/12, 2008 at 17:2 Comment(3)
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.Thaumatology
@Thaumatology 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.Banquer
I'm using 3.5...still not fixed. ;)Thaumatology
B
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.

Banquer answered 13/2, 2009 at 18:49 Comment(1)
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.Adrenal
S
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

Stupefacient answered 8/1, 2009 at 0:20 Comment(4)
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 firingBanquer
It works for me: private void dgvPlatypus_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e) { e.ToolTipText = "j. cutworm is a punk"; }Puttee
CellToolTipTextNeeded is not getting fired . Any tip for it ?Pisolite
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"; }Cetus
D
5

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.

Discount answered 29/4, 2010 at 21:38 Comment(0)
W
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.

Walliw answered 11/2, 2010 at 9:35 Comment(1)
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."Signpost
L
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();
    }
}
Lazar answered 31/10, 2015 at 8:17 Comment(0)
B
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.

Banquer answered 13/2, 2009 at 18:49 Comment(1)
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.Adrenal
W
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.";
Whelp answered 16/5, 2012 at 23:43 Comment(0)
P
2

Set datagridview ShowCellToolTips property to False

Pneumatograph answered 7/1, 2016 at 20:5 Comment(1)
The opposite worked for me - by setting dgv.ShowCellToolTips = true; the column header tooltips then started to display.Dreamworld
G
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.

Grandniece answered 19/3, 2010 at 14:23 Comment(1)
@sindre In my case, the DataSource is definitely set, otherwise there would be no data in my GridView.Banquer
E
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.

Enmity answered 30/12, 2008 at 7:55 Comment(1)
@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.Banquer
P
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;
            }
        }
    }
Philippopolis answered 12/8, 2010 at 16:4 Comment(1)
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")Philippopolis
S
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");
    }
    
Spelldown answered 8/9, 2015 at 5:23 Comment(0)
F
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.

Funkhouser answered 12/11, 2018 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.