DataGridView ToolTipText not showing

不想你离开。 提交于 2019-12-04 02:35:55

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

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.

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.

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.";
Muthukumar K

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();
    }
}
Robert Gowland

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.

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.

Set datagridview ShowCellToolTips property to False

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.

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;
            }
        }
    }
Alex Jolig
  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");
    }
    

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!