DataGridView ToolTipText未显示

时间:2008-12-23 17:02:11

标签: c# visual-studio visual-studio-2008 datagridview desktop-application

我的桌面应用中的数据绑定DataGridView,其列设置了ToolTipText属性,但当我将鼠标悬停在网格视图(单元格或单元格标题)上时,不会显示工具提示。

网格视图的ShowCellToolTips属性为true,我已经使用断点验证了在鼠标悬停之前它没有以编程方式更改。

我尝试创建一个CellToolTipTextNeeded事件处理程序来查看工具提示文本是什么,但从不调用事件处理程序。

我有什么遗漏的吗?

谢谢, 罗布

编辑:我们正在使用框架2.0。

12 个答案:

答案 0 :(得分:9)

从您的问题中可以看出,您设置了列的工具提示文本。 列工具提示文本仅在标题上方浮动时显示。要在单元格上显示工具提示文本,您必须连接CellToolTipTextNeeded事件并在事件args中设置e.ToolTipText的值

答案 1 :(得分:4)

尝试使用Cell.ToolTipText属性。您可能需要循环DataGridView的行并手动设置工具提示:

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

可能不适合具有大量行的绑定DataGridView,但对于我来说,使用带有几百行的未绑定DataGridView可以成功运行。希望这会有所帮助。

答案 2 :(得分:4)

当我将带有单个(空)列的datagridview添加到表单时,将该文本添加到该列的ToolTipText属性中,并确保将datagridview的ShowCellToolTips属性设置为True,我确实会获得工具提示弹出窗口我将鼠标悬停在该列的标题上。这似乎与原始问题中陈述的内容相矛盾,但在我的测试中,网格不是数据绑定的。不确定这是否有所作为。但是,在具有数据绑定datagridview的项目中,我只使用了ToolTip组件:

(1)向表单添加ToolTip组件。
(2)将datagridview的ToolTip on toolTip1(或ToolTip组件的等效名称)属性设置为您要显示的文本。
(3)将datagridview的ShowCellToolTips属性设置为False (4)中提琴!按预期工作。

答案 3 :(得分:3)

要显示网格单元格的工具提示,您可以使用此事件处理程序“ CellToolTipTextNeeded ”。请参阅以下代码片段,

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

答案 4 :(得分:2)

我们最终使用工具提示窗口小部件和CellMouseEnterCellMouseLeave事件来正确显示它。不是最佳的,但它可以解决我们遇到的奇怪行为。

答案 5 :(得分:2)

我有一个类似的问题但是能够通过在我的DataGridView上将ShowCellToolTip设置为true来纠正它。一旦我这样做,我能够发送以下代码,一切正常。

tableDocTypes.ShowCellToolTips = true;
tableDocTypes.Rows[i].Cells[columnFormCabinet.Index].ToolTipText = "Cabinet is not defined on the optical server.";

答案 6 :(得分:1)

我目前在Framework 3.5上遇到了同样的问题。似乎需要设置DataSource属性才能触发CelToolTipTextNeeded事件。

答案 7 :(得分:1)

将datagridview ShowCellToolTips属性设置为False

答案 8 :(得分:0)

我不知道这个提示是否是您特定问题的解决方案,但您使用的是VS2008的SP1吗? 正如我所发现的,此Service Pack解决了许多不同的问题。

答案 9 :(得分:0)

我发现这篇文章在寻找每行设置工具提示的帮助。

我只是想确认在VS2008 SP1中处理CellToolTipText事件对我有效。

对于那些想知道如何将文本设置为基础数据行中的值的人,这可能很有用:

    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;
            }
        }
    }

答案 10 :(得分:0)

  1. 将您的DataGridView的ShowCellToolTips媒体资源设为false
  2. 将此代码放入DataGridView的CellMouseEnter事件

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

答案 11 :(得分:0)

有一个相关的问题,其中CellToolTipTextNeeded只会偶尔被调用。当单元中有溢出时,行为是提示而不是提示。 如果单元格的WrapMode设置为true,则每次都会正确调用CellToolTipTextNeeded。 我以为可以调用CellToolTipTextNeeded并覆盖通用工具提示,但似乎仅在datagridview.cell的第一个条目中调用此事件,然后,如果将鼠标移出该单元并再次打开(保留在datagridview中),则“查看溢出工具提示”,而是显示...

无论如何,信息可能会帮助其他人。